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. Python函数式编程(一):高级函数

    首先有一个高级函数的知识. 一个函数可以接收另一个函数作为参数,这种函数就称之为高阶函数. def add(x, y, f): return f(x) + f(y) 当我们调用add(-, , abs ...

  2. mac上Docker安装&初体验

    Docker是什么? Docker是一个虚拟环境容器,可以将你的开发环境.代码.配置文件等一并打包到这个容器中,并发布和应用到任意平台中. 官方文档:https://docs.docker.com H ...

  3. js获取地址栏中的数据

    window.location.href:设置或获取整个 URL 为字符串window.location.pathname:设置或获取对象指定的文件名或路径window.location.search ...

  4. idea2018版tomcat基本配置

    前言 在配置tomcat之前,要先创建一个javaweb的工程 打开idea的主界面,在菜单中点击File,出现以下的图 点击选择 Application Server 点击选择 Tomcat Ser ...

  5. Solidity高级理论(二):Gas

    solidity高级理论(二):Gas 关键字:Gas.结构体.节省小技巧 Gas是什么 在Solidity中,用户想要执行DApp都需要支付一定量的Gas,Gas可以用以太币购买,所以,用户每次使用 ...

  6. 下载Eclipse、下载Java各个版本,来这里就对了

    Eclipse官网:http://www.eclipse.org/ 不信你去看看 Java官网:https://www.java.com/ 不信你去看看 可惜是,每次进入官网提示都是下面这样的:来,我 ...

  7. May 24. 2018 Week 21st Thursday

    Man errs so long as he strives. 失误是进取的代价. It is not important that the man in the arena didn't win, ...

  8. Cobalt Strike 服务器搭建及使用

    Cobalt Strike使用中的一些坑(一) http://www.cnblogs.com/miaodaren/articles/7829793.html cobaltstrike3.8服务器搭建及 ...

  9. input reset 重置时间

    经验规律,301毫秒. function autoFormatMoney() { if (!this.value.length) {return} var num = parseFloat(this. ...

  10. 「2017 山东一轮集训 Day5」苹果树

    「2017 山东一轮集训 Day5」苹果树 \(n\leq 40\) 折半搜索+矩阵树定理. 没有想到折半搜索. 首先我们先枚举\(k\)个好点,我们让它们一定没有用的.要满足这个条件就要使它只能和坏 ...