Skip to content

@CacheEvict will remove all keys of current database  #2028

@ramonli

Description

@ramonli

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;
    }

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.

Metadata

Metadata

Assignees

Labels

for: stackoverflowA question that's better suited to stackoverflow.com

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions