一、添加依赖

  <!-- 添加缓存支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency> <!-- 添加Redis缓存支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> <!-- 工具类 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>

二、配置Redis数据库

spring:
redis:
#数据库索引
database: 1
host: 192.168.2.230
port: 6379
password:
jedis:
pool:
#最大连接数
max-active: 8
#最大阻塞等待时间(负数表示没限制)
max-wait: -1
#最大空闲
max-idle: 8
#最小空闲
min-idle: 0
#连接超时时间
timeout: 10000

三、Redis配置类

package com.hn.lz.config;

import com.hn.lz.handler.FastJsonRedisSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.RedisSerializationContext; import java.lang.reflect.Method;
import java.time.Duration; /**
* Created by mll on 2018/7/16.
*/
@Configuration
@EnableCaching //开启支持缓存
public class RedisConfiguration extends CachingConfigurerSupport { static Logger logger = LoggerFactory.getLogger(RedisConfiguration.class); /**
* 自定义生成key的规则
*
* @return
*/
@Override
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object o, Method method, Object... objects) {
//格式化缓存key字符串
StringBuilder sb = new StringBuilder();
//追加类名
sb.append(o.getClass().getName()).append(".");
//追加方法名
sb.append(method.getName());
//遍历参数并且追加
for (Object obj : objects) {
sb.append(".");
sb.append(obj.toString());
}
System.out.println("调用Redis缓存Key : " + sb.toString());
return sb.toString();
}
};
} /**
* 设置 redis 数据默认过期时间
* 设置 Cache 序列化方式
*
* @return
*/
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
configuration = configuration
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer))
.entryTtl(Duration.ofDays(30));
return configuration;
} }

  

四、自定义存入Redis数据库值的序列化方式

package com.hn.lz.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException; import java.nio.charset.Charset; /**
* Created by mll on 2018/7/17.
*/
public class FastJsonRedisSerializer<T> implements RedisSerializer<T> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private Class<T> clazz; public FastJsonRedisSerializer(Class<T> clazz) {
super();
this.clazz = clazz;
} @Override
public byte[] serialize(T t) throws SerializationException {
if (null == t) {
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
} @Override
public T deserialize(byte[] bytes) throws SerializationException {
if (null == bytes || bytes.length <= 0) {
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return (T)JSON.parseObject(str, clazz);
} }

  

五、使用注解来缓存数据

package com.hn.lz.service;

import com.hn.lz.mapper.UserMapper;
import com.hn.lz.model.User;
import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.transaction.annotation.Transactional; import java.util.List; /**
* Created by mll on 2018/6/21.
*/
@CacheConfig(cacheNames = "user")
@Service
public class UserService { @Autowired
private UserMapper userMapper; // ---------------------------------- 数据操作 BEGIN --------------------------------------
/**
* 插入数据
* @param data
* @return
*/
@Transactional
@CachePut(key = "#data.id")
public User insert(User data){
userMapper.insert(data);
return data;
} /**
* 更新数据
* @param data
* @return
*/
@Transactional
@CachePut(key = "#data.id")
public User update(User data){
userMapper.updateByPrimaryKeySelective(data);
return data;
} /**
* 删除数据
* @param id
* @return
*/
@Transactional
@CacheEvict(key = "#id")
public int delete(String id){
int result = userMapper.deleteByPrimaryKey(id);
return result;
} /**
* 得到所有数据列表
* @return
*/
public List<User> select() {
List<User> list = userMapper.selectAll();
return list;
} /**
* 根据id查询数据
* @param id
* @return
*/
@Cacheable(key = "#id")
public User query(String id){
User data = userMapper.selectByPrimaryKey(id);
return data;
}
// ---------------------------------- 数据操作 END --------------------------------------
}

  

六、要点

1、注解

  • @CacheConfig这个注解主要用于配置该类中会用到的一些公用的缓存配置。我们也可以不使用该注解,直接通过自己的注解配置缓存集的名字来定义。
@CacheConfig(cacheNames = "user")
@Service
public class UserService

  

  • @CachePut这个注解直接将返回值放入缓存中,通常用于保存和修改方法中。
  • value缓存的名称,必须指定至少一个。
  • key缓存的 key,可以为空,如果指定要按照 SpEL(Spring Expression Language) 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合。
  • condition:缓存的条件,可以为空,使用 SpEL 编写,返回true或者false只有为true才进行缓存。
@CachePut(value = "user", key = "#data.id", condition = "#result.username ne 'zhang'")
public User insert(User data)

  

  • @Cacheable这个注解在执行前先查看缓存中是不是已经存在了,如果存在,直接返回。如果不存在,将方法的返回值放入缓存。
  • value缓存的名称,必须指定至少一个。
  • key缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合。
  • condition缓存的条件,可以为空,使用 SpEL 编写,返回true或者false只有为true才进行缓存。
@Cacheable(value = "user", key = "#id", condition = "#id lt 10")
public User query(Long id)

  

  • @CacheEvict这个注解在执行方法执行成功后会从缓存中移除。
  • value缓存的名称,必须指定至少一个。
  • key缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合。
  • condition缓存的条件,可以为空,使用 SpEL 编写,返回true或者false只有为true才进行缓存。
  • allEntries是否清空所有缓存内容,缺省为false如果指定为true则方法调用后将立即清空所有缓存。
  • beforeInvocation是否在方法执行前就清空,缺省为false如果指定为true则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存。
@CacheEvict(value = "user", key = "#data.id", beforeInvocation = false, condition = "#result.username ne 'zhang'")
public User delete(User data)

  

  • @Caching这个注解组合多个Cache注解使用,并且可以自定义注解使用。
@Caching(
put = {
@CachePut(value = "user", key = "#user.id"),
@CachePut(value = "user", key = "#user.username"),
@CachePut(value = "user", key = "#user.email")
}
)
@Caching(
put = {
@CachePut(value = "user", key = "#user.id"),
@CachePut(value = "user", key = "#user.username"),
@CachePut(value = "user", key = "#user.email")
}
)
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface MyCache {
} @MyCache
public User save(User data)

  

2、Spring Cache提供了一些供我们使用的SpEL上下文数据

名字 描述 示例
methodName 当前被调用的方法名 #root.methodName
method 当前被调用的方法 #root.method.name
target 当前被调用的目标对象 #root.target
targetClass 当前被调用的目标对象类 #root.targetClass
args 当前被调用的方法的参数列表 #root.args[0]
caches 当前方法调用使用的缓存列表(如@Cacheable(value={"cache1", "cache2"})),则有两个cache #root.caches[0].name
argument name 当前被调用的方法的参数,如findById(Long id),我们可以通过#id拿到参数 #user.id
result 方法执行后的返回值(仅当方法执行之后的判断有效,如‘unless’,'cache evict'的beforeInvocation=false) #result

3、Redis常用命令

flushdb:清空当前数据库。
select index:选择索引数据库,index为索引值名,如:select 1
del key:删除一条指定key的值。
keys *:查看数据库内所有的key。
flushall:清空所有数据库。
quit:退出客户端连接。

作者:maololo
链接:https://www.jianshu.com/p/e67e85eb63b2
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

SpringBoot 2.x 使用Redis作为项目数据缓存的更多相关文章

  1. springboot(12)Redis作为SpringBoot项目数据缓存

    简介: 在项目中设计数据访问的时候往往都是采用直接访问数据库,采用数据库连接池来实现,但是如果我们的项目访问量过大或者访问过于频繁,将会对我们的数据库带来很大的压力.为了解决这个问题从而redis数据 ...

  2. 【快学SpringBoot】Spring Cache+Redis实现高可用缓存解决方案

    前言 之前已经写过一篇文章介绍SpringBoot整合Spring Cache,SpringBoot默认使用的是ConcurrentMapCacheManager,在实际项目中,我们需要一个高可用的. ...

  3. Memcache,Redis,MongoDB(数据缓存系统)方案对比与分析

    mongodb和memcached不是一个范畴内的东西.mongodb是文档型的非关系型数据库,其优势在于查询功能比较强大,能存储海量数据.mongodb和memcached不存在谁替换谁的问题. 和 ...

  4. 【redis】4.spring boot集成redis,实现数据缓存

    参考地址:https://spring.io/guides/gs/messaging-redis/ ================================================== ...

  5. SpringBoot学习笔记(6) SpringBoot数据缓存Cache [Guava和Redis实现]

    https://blog.csdn.net/a67474506/article/details/52608855 Spring定义了org.springframework.cache.CacheMan ...

  6. SpringBoot 结合 Spring Cache 操作 Redis 实现数据缓存

    系统环境: Redis 版本:5.0.7 SpringBoot 版本:2.2.2.RELEASE 参考地址: Redus 官方网址:https://redis.io/ 博文示例项目 Github 地址 ...

  7. 项目总结10:通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题

    通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题 关键字 springboot热部署  ClassCastException异常 反射 ...

  8. springboot中如何向redis缓存中存入数据

    package com.hope;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jack ...

  9. Shiro整合springboot,freemaker,redis(含权限系统完整源码)

    区块链技术联盟 2018-02-08 17:06:40 目录 一.导语 二.shiro功能介绍 三.shiro详解 四.shiro实战案例分享 五.系统配置 六.其他 一.导语 今天推荐给大家一个非常 ...

随机推荐

  1. Spring的基本应用(1):依赖以及控制反转

    在说到这里的时候,首先要说下程序的耦合和解耦,以便对上节做一个解释. 一.程序的耦合和解耦 1.程序的耦合性(Copling) (1)程序的耦合性,也叫做耦合度,是对模块之间关联程度的度量,耦合性的强 ...

  2. 两个实体类 复制 copy 工具类

    import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Date; public class ...

  3. pytorch简单框架

    网络搭建: mynn.py: import torchfrom torch import nnclass mynn(nn.Module): def __init__(self): super(mynn ...

  4. python、第六篇:视图、触发器、事务、存储过程、函数

    一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...

  5. 第十篇.1、python并发编程之多进程理论部分

    一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): egon在一个时间段内有很多任务要做:python备课的任务,写书的任 ...

  6. 51单片机通过ESP8266模块与手机进行通讯(单片机)

    相关连接和资料下载: 个人博客 资料下载 Step1:配置ESP8266 通过USB转TTL模块把ESP8266模块和电脑连接起来,如图: 把ESP8266模块的VCC,GND,CH_PD,UTXD, ...

  7. 多线程-生产者消费者(lock同步)

    二.采用Lock锁以及await和signal方法是实现 import java.io.IOException; import java.util.concurrent.locks.Condition ...

  8. AWR报告提取方法

    AWR报告提取方法 关键字 AWR报告 内容描述 AWR报告的提取方法 涉及设备 oracle 10g 操作说明 [问题现象描述] 现场提取AWR报告 [分析结论及解决方案] Awr报告生成方法 进入 ...

  9. 2017 网易游戏互娱游戏研发4.21(offer)

    网易游戏互娱(offer) 去年这个时候就参加过网易游戏的实习生招聘,到今年总共收到了4次拒信.不过这次运气好,终于get了最想要的offer.去年实习生互娱笔试挂,秋招笔试挂,今年春招互娱投了连笔试 ...

  10. buuctf@[OGeek2019]babyrop

    #!/usr/bin/python #coding:utf-8 from pwn import * #context.log_level = 'debug' io = process('./pwn', ...