Spring-Cache manually clear the cache

Spring Cache Redis manually clear the cache

Registration cacheRedisTemplate

The cache of RedisTemplate registered as Bean

@Bean(name = "cacheRedisTemplate")
public RedisTemplate cacheRedisTemplate(@Qualifier("jedisConnectionFactory") JedisConnectionFactory jedisConnectionFactory,
                                            @Qualifier("jedisYsfKeySerializer") RedisSerializer jedisYsfKeySerializer,
                                            @Qualifier("jedisYsfValueSerializer") RedisSerializer jedisYsfValueSerializer) {
    RedisTemplate cacheRedisTemplate = new RedisTemplate();
    cacheRedisTemplate.setConnectionFactory(jedisConnectionFactory);
    cacheRedisTemplate.setEnableTransactionSupport(false);
    cacheRedisTemplate.setKeySerializer(jedisYsfKeySerializer);
    cacheRedisTemplate.setValueSerializer(jedisYsfValueSerializer);
    return cacheRedisTemplate;
}

Use cacheRedisTemplate

Then clear the cache, it can be used directly cacheRedisTemplate.

....

@Autowired 
@Qualifier("cacheRedisTemplate") 
var cacheRedisTemplate : YsfRedisTemplate[String,Object] = _

...

cacheRedisTemplate.delete(s"cc.c.sta.real.in.day:$id-$start-$i")

Here is the syntax of the scala, java empathy

Other write cache, check the cache, all the same operation.

Singular usage

Add annotations on Mybatis the Dao.

@CacheEvict(value = CACHE_PREFIX,key = "#root.args[0]+'-'+#root.args[1]+'-'+#root.args[2]")
int clearCache(@Param("id") Long id,
            @Param("start") Long start,
            @Param("type") Integer type)  throws DataAccessException;

Mybatis of sql are the following:

<select id="clearCache" resultType="int">
    select 1
</select>

This operation is not substantial database operations, just to delete the cache. 6666, after all, a way ....

Guess you like

Origin www.cnblogs.com/ElEGenT/p/11770408.html