Skip to content

@CacheEvict will remove all keys in current database not only the cache. #2029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ramonli opened this issue Apr 9, 2021 · 1 comment
Closed
Labels
status: duplicate A duplicate of another issue

Comments

@ramonli
Copy link

ramonli commented Apr 9, 2021

Recently we upgraded spring-data-redis from 1.X to 2.2.6, and found that @CacheEvict didn't work as we understood in 1.X. Below is our sample code.

    @Cacheable(value = "PORTAL_OPERATION_SERVER:CONTENT_CHANNEL_TAG")
    @Override
    public ContentChannelTag queryByPrimaryKey(Long id) {
        return contentChannelTagMapper.selectByPrimaryKey(id);
    }

    @CacheEvict(value = "PORTAL_OPERATION_SERVER:CONTENT_CHANNEL_TAG", allEntries = true)
    @Override
    public int deleteByPrimaryKey(Long id) {
        int i = contentChannelTagMapper.deleteByPrimaryKey(id);
        contentChannelTagArticleMergeMapper.deleteByTagId(id); 
        return i;
    }

we set usePrefix=false

and a simple testcase:

@Test
public void selectAndDeleteByPrimaryKey() throws Exception {
    ContentChannelTag tag = this.contentChannelTagService.queryByPrimaryKey(7l);
    logger.debug("RESP:{}", JsonUtils.beanToJson(tag));
    int count = this.contentChannelTagService.deleteByPrimaryKey(7l);
    logger.debug("COUNT:{}", count);
}

We monitor the redis commands when running the test cases, the redis commands are as below:

1617933749.723764 [2 192.168.16.29:63711] "INFO"
1617933750.060568 [2 192.168.16.29:63711] "PING"
1617933750.069994 [2 192.168.16.29:63711] "GET" "queryByPrimaryKey:bd703dc0b11593277a5a82dd893f2880b8d0f32a"
1617933750.158564 [2 192.168.16.29:63711] "PING"
1617933750.170874 [2 192.168.16.29:63711] "SET" "queryByPrimaryKey:bd703dc0b11593277a5a82dd893f2880b8d0f32a" "\xac\xed\x00\x05sr\x00Ccom.huize.portal.operation.domain.contentchannel2.ContentChannelTag\xf44^%\xb2h_\xa4\x02\x00\nL\x00\x10createOperatorIdt\x00\x10Ljava/lang/Long;L\x00\x12createOperatorNamet\x00\x12Ljava/lang/String;L\x00\x05isHott\x00\x10Ljava/lang/Byte;L\x00\x16isShowTopAdvertisementq\x00~\x00\x03L\x00\x04nameq\x00~\x00\x02L\x00\x04sortq\x00~\x00\x01L\x00\x05stateq\x00~\x00\x03L\x00\x12topAdvertisementIdq\x00~\x00\x01L\x00\x10updateOperatorIdq\x00~\x00\x01L\x00\x12updateOperatorNameq\x00~\x00\x02xr\x00"com.huize.portal.common.BaseEntity\x82\xb3\r1h\x91\xd36\x02\x00\x04L\x00\ncreateTimet\x00\x10Ljava/util/Date;L\x00\adeletedt\x00\x13Ljava/lang/Boolean;L\x00\x02idq\x00~\x00\x01L\x00\nupdateTimeq\x00~\x00\x05xpsr\x00\x0ejava.util.Datehj\x81\x01KYt\x19\x03\x00\x00xpw\b\x00\x00\x01e\xc6Y\x0f\x10xsr\x00\x11java.lang.Boolean\xcd r\x80\xd5\x9c\xfa\xee\x02\x00\x01Z\x00\x05valuexp\x00sr\x00\x0ejava.lang.Long;\x8b\xe4\x90\xcc\x8f#\xdf\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x00\x00\x00\x00\x00\asq\x00~\x00\bw\b\x00\x00\x01e\xcb\x7fk\x10xsq\x00~\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x00t\x00\x00sr\x00\x0ejava.lang.Byte\x9cN`\x84\xeeP\xf5\x1c\x02\x00\x01B\x00\x05valuexq\x00~\x00\r\x00sq\x00~\x00\x12\x01t\x00\r\xe6\x8e\x92\xe5\xba\x8f\xe6\xb5\x8b\xe8\xaf\x951q\x00~\x00\x0eq\x00~\x00\x14sq\x00~\x00\x0c\x00\x00\x00\x00\x00\x00\x00Tsq\x00~\x00\x0c\x00\x00\x00\x00\x00\x00\x00\xdft\x00\t\xe6\xb6\x82\xe9\x92\xb1\xe8\x8b\xb1" "px" "300000"
1617933750.238104 [2 192.168.16.29:63711] "PING"
1617933750.247365 [2 192.168.16.29:63711] "KEYS" "*"
1617933750.255121 [2 192.168.16.29:63711] "DEL" "test" "name" "age" "queryByPrimaryKey:bd703dc0b11593277a5a82dd893f2880b8d0f32a"

What surprised us was that deleteByPrimaryKey will remove all keys in database #2, in our understanding it should only remove all keys in cache PORTAL_OPERATION_SERVER:CONTENT_CHANNEL_TAG.

We found this commit a9c2b1c#diff-3e7270895c24c01ca01a37cc00a9a27cb48e03d2b2d969a764d31b5e29345ea8 which introduced a huge change to RedisCache, seem it doesn't care the Cache any more.

Please let us know whether we got a misunderstanding.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 9, 2021
@mp911de mp911de added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 9, 2021
@mp911de
Copy link
Member

mp911de commented Apr 9, 2021

Closing as duplicate of #2028.

@mp911de mp911de closed this as completed Apr 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants