package org.cheng.user.client.service;

import java.util.HashMap;
import java.util.Map; import org.cheng.user.client.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; /**
* 统一命名缓存 名称
* @author Cheng
*/
@CacheConfig(cacheNames = { "user" })
@Service
public class UserService { @Autowired
private CacheManager cacheManager; /**
* 单个命名缓存名称
* sync 默认为false 为true 需要同步等待
* @param id
* @return
*/
@Cacheable(cacheNames = "user", key = "#id",sync = false)
public User get(Integer id) {
System.out.println("--get---");
User user = new User();
user.setId(id);
return user;
} /**
* 按key存储
* 前置存储条件 condition 为true 存储
* 后置执行条件 unless 为false 存储
*
* @param user
* @return
*/
@CachePut(key = "#user.id", condition = "#user.id<10000", unless = "#result.name.length() < 3")
public User save(User user) {
user.setName("name - " + user.getId());
return user;
} /**
* 按key删除
*
* @param id
* @return
*/
@CacheEvict(key = "#id")
public String del(Integer id) {
System.out.println("--del---");
return String.valueOf(id);
} /**
* 清除全部
*/
@CacheEvict(allEntries = true)
public void removeAll() {
} /**
* 返回所有的缓存信息
* @return
*/
public Map<String,Cache> cache() {
Map<String,Cache> map = new HashMap<>();
for(String name:cacheManager.getCacheNames()) {
map.put(name, cacheManager.getCache(name));
}
return map;
} }

参考文献:

https://docs.spring.io/spring/docs/5.1.6.RELEASE/spring-framework-reference/integration.html#cache

https://www.cnblogs.com/OnlyCT/p/7845660.html#t4

Spring cache 使用说明的更多相关文章

  1. Spring Cache 笔记

    @(Java ThirdParty)[Spring Cache] Spring Cache Abstraction 简介 Spring Cache提供了对底层缓存使用的抽象,通过注解的方式使用缓存,减 ...

  2. Spring Cache缓存框架

    一.序言 Spring Cache是Spring体系下标准化缓存框架.Spring Cache有如下优势: 缓存品种多 支持缓存品种多,常见缓存Redis.EhCache.Caffeine均支持.它们 ...

  3. Spring cache简单使用guava cache

    Spring cache简单使用 前言 spring有一套和各种缓存的集成方式.类似于sl4j,你可以选择log框架实现,也一样可以实现缓存实现,比如ehcache,guava cache. [TOC ...

  4. 注释驱动的 Spring cache 缓存介绍

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  5. [转]注释驱动的 Spring cache 缓存介绍

    原文:http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ 概述 Spring 3.1 引入了激动人心的基于注释(an ...

  6. Spring Cache 介绍

    Spring Cache 缓存是实际工作中非常常用的一种提高性能的方法, 我们会在许多场景下来使用缓存. 本文通过一个简单的例子进行展开,通过对比我们原来的自定义缓存和 spring 的基于注释的 c ...

  7. spring Cache注解

    如下:不能将缓存注解加在listCate(boolean isShowHide)方法上 因为spring是使用AOP的方法获取缓存,在一个bean中再去调用别一个方法,不会应用缓存 @Cacheabl ...

  8. 注释驱动的 Spring cache 缓存介绍--转载

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  9. Spring Cache使用详解

    Spring Cache Spring Cache使用方法与Spring对事务管理的配置相似.Spring Cache的核心就是对某个方法进行缓存,其实质就是缓存该方法的返回结果,并把方法参数和结果用 ...

随机推荐

  1. MS SQL批量生成作业脚本方法介绍总结

    在迁移或升级SQL Server数据库服务器时,很多场景下我们不能还原msdb,所以我们必须手工迁移SQL Server相关作业.如果手工生成每一个作业的脚本话,费时又费力,其实SQL Server中 ...

  2. BurpSuit2.0专业版破解

    简介 Burp Suite 是用于攻击web 应用程序的集成平台.它包含了许多Burp工具,这些不同的burp工具通过协同工作,有效的分享信息,支持以某种工具中的信息为基础供另一种工具使用的方式发起攻 ...

  3. mssql sqlserver 关键字 GROUPING用法简介及说明

    转自: http://www.maomao365.com/?p=6208  摘要: GROUPING 用于区分列是否由 ROLLUP.CUBE 或 GROUPING SETS 聚合而产生的行 如果是原 ...

  4. 语句调优基础知识-set statistics io on

    set statistics io on --清空缓存数据 dbcc dropcleanbuffers go --清空缓存计划 dbcc freeproccache go set statistics ...

  5. Function、Object、Prototype 之间的关系解析

    前提:js中所有的内置对象都是Function 的实例. 例如:Array\String\Number... 等 原理剖析: 对象属性搜索的原理就是按照对象的 __proto__ 属性进行搜索,直到_ ...

  6. org.hibernate.NonUniqueObjectException

    错误如下: 2017-3-29 15:17:52~ERROR~org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV ...

  7. AFURLSessionManager 上传下载使用

    1.下载 Creating a Download Task NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ...

  8. no plugin found for prefix 'tomcat 7' in the current project and in the plugin groups的解决方法

    解决方法一: 找到这个settings.xml文件,进行编辑,在pluginGroups标签下加入下面的配置 <pluginGroups><pluginGroup>org.ap ...

  9. IPerf——网络测试工具介绍与源码解析(3)

    [线程的生成]   生成线程时需要传入一个thread_Settings类型的变量,thread_Settings包含所有线程运行时需要的信息,命令行选项参数解析后所有得到的属性都存储到该类型的变量中 ...

  10. 联想x3650m5服务器安装windows2008R2系统

    服务器型号:联想x3650 M5 2U服务器 硬盘:一块300G硬盘 阵列:raid0 系统:windowsserver2008R2系统 安装开始时间:20180930晚上9点 客户手里有window ...