栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用spring cache缓存学习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用spring cache缓存学习

spring boot cache 提供了一些注解方便做cache应用。

(1)@CacheConfig:主要用于配置该类中会用到的一些共用的缓存配置

(2)@Cacheable:主要方法返回值加入缓存。同时在查询时,会先从缓存中取,若不存在才再发起对数据库的访问。

(3)@CachePut:配置于函数上,能够根据参数定义条件进行缓存,与@Cacheable不同的是,每次回真实调用函数,所以主要用于数据新增和修改操作上。

(4)@CacheEvict:配置于函数上,通常用在删除方法上,用来从缓存中移除对应数据

(5)@Caching:配置于函数上,组合多个Cache注解使用。

注意:

cache相关注解必须在spring所管理的bean容器中,这样才能使缓存生效、启动类中要加入@EnableCaching注解支持缓存

用例如下:

@Service

@CacheConfig(cacheNames = "testCache", cacheManager = "activitymetaRedisCacheManager")

public class TestCacheService implements TestCacheService {

 @CachePut(key = "'meta:'+#activityId", unless = "#result == null")

    public ActivitymetaDO incrementCount(String activityId, Verb incrementVerb, int inc) {

        boolean r = activitymetaStoreBuilder.incrementCount(activityId, incrementVerb, inc);

        if (r) {

            ActivitymetaDO activitymetaDO = new ActivitymetaDO();

            activitymetaDO.setActivityId(activityId);

            activitymetaDO = activitymetaStoreBuilder.getByPrimaryKey(activitymetaDO);

            return activitymetaDO;

        }

        return null;

    }

   

    @Cacheable(key = "'meta:'+#activityId", unless = "#result == null")

    @Override

    public ActivitymetaDO getByViewId(String activityId) {

        ActivitymetaDO activitymetaDO = new ActivitymetaDO();

        activitymetaDO.setActivityId(activityId);

        return activitymetaStoreBuilder.getByPrimaryKey(activitymetaDO);

    }

   

     @CachePut(key = "'meta:'+#meta.activityId", unless = "#result == null")

    @Override

    public ActivitymetaDO save(ActivitymetaDO meta) {

        return activitymetaStoreBuilder.store(meta);

    }

   

     @CacheEvict(key = "'meta:'+#activityId")

    @Override

    public boolean deleteByViewId(String activityId) {

        ActivitymetaDO meta = new ActivitymetaDO();

        meta.setActivityId(activityId);

        String recommendKey = CacheKey.ACTIVITY_meta_RECOMMEND + CustomerConstants.CountryLanguage.fromCountry(meta.getCountry()).getLanguageZone();

        long delResult = recommendRedisTemplate.opsForZSet().remove(recommendKey, activityId);

        return activitymetaStoreBuilder.deleteByPrimaryKey(meta);

    }

   

    //定义缓存RedisCacheManager对象,在缓存时使用

     @Bean("activitymetaRedisCacheManager")

    @Primary

    public RedisCacheManager activitymetaRedisCacheManager(RedisConnectionFactory redisConnectionFactory) {

        RedisCacheConfiguration cacheConfig = RedisCacheConfiguration.defaultCacheConfig();

        cacheConfig = buildCacheConfig(cacheConfig);

        return new SocialRedisCacheManger(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory), cacheConfig);

    }

   

    相关优秀文章:

    https://blog.csdn.net/zlj1217/article/details/80928122

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/301530.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号