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. Android 组件的三种点击事件写法

    一.准备工作 1.项目结构 2.布局文件(activity_main.xml) <?xml version="1.0" encoding="utf-8"? ...

  2. Visual Studio未能加载“XX”包的解决方案

    1.打开命令行(管理员) 2.进入到devenv.exe所在的目录u 3.输入devenv,会出现帮助提示 4.将所有带“Reset..”的命令执行一遍 5.OK 我尝试过使用修复功能,貌似没有作用. ...

  3. Jetbrains Idea连接TFS时配置的坑

    #Team Explorer Everywherehttps://www.microsoft.com/en-us/search/result.aspx?q=team+explorer+everywhe ...

  4. 使用VSTS的Git进行版本控制(四)——在Visual Studio中管理分支

    使用VSTS的Git进行版本控制(四)--在Visual Studio中管理分支 可以从web版Team Services Git repo 的Branches视图中管理工作.定制视图来跟踪最关注的分 ...

  5. openstack nova工作流程

    工作流程请求:nova boot --image ttylinux --flavor 1 i-01nova-api 接受请求,一个tcp REST请求.nova-api 发送一个创建虚拟机的请求到消息 ...

  6. linux:644、755、777权限详解

    第一位7等于4+2+1,rwx,所有者具有读取.写入.执行权限: 第二位5等于4+1+0,r-x,同组用户具有读取.执行权限但没有写入权限: 第三位5,同上,也是r-x,其他用户具有读取.执行权限但没 ...

  7. C#-泛型类型(十六)

    概述 泛型类和泛型方法兼具可重用性.类型安全性和效率,这是非泛型类和非泛型方法无法实现的 泛型通常与集合以及作用于集合的方法一起使用 泛型所属命名空间:System.Collections.Gener ...

  8. C#面向对象 1

    using System; using System.Collections.Generic; using System.Collections; using System.Linq; using S ...

  9. Django之--通过MVC架构的html模板展示Hello World!

    上一篇:Django之--网页展示Hello World! 初步说明了如何使用Django来显示hello world,本文略微进阶下使用html模板来展示hello world~ 首先在mysite ...

  10. python轻量级数据存储

    python为开发者提供了一个轻量级的数据存储方式shelve,对于一些轻量数据,使用shelve是个比较不错的方式.对于shelve,可以看成是一个字典,它将数据以文件的形式存在本地.下面介绍具体用 ...