只要加入spring-boot-starter-data-redis , springboot 会自动识别并使用redis作为缓存容器,使用方式如下

gradle加入依赖

    compile("org.springframework.boot:spring-boot-starter-data-redis:${springBootVersion}")

redis configuration 中启用缓存

@Configuration
@EnableCaching
public class RedisConfiguration

redis 自定义key生成规则

@Bean
public KeyGenerator wiselyKeyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName());
sb.append(":" + method.getName());
for (Object obj : params) {
sb.append(":" + obj.toString());
}
return sb.toString();
}
}; }

有时候我们需要 使用 redisTemplate, 可以这样子配置

@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new JedisConnectionFactory();
} @Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
} @Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
return new RedisCacheManager(redisTemplate);
}

完整代码

package cn.xiaojf.today.data.redis.configuration;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate; import java.lang.reflect.Method; /**
* redis 缓存配置
* @author xiaojf 2016/12/7 10:29.
*/
@Configuration
@EnableCaching
public class RedisConfiguration { @Bean
public KeyGenerator wiselyKeyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName());
sb.append(":" + method.getName());
for (Object obj : params) {
sb.append(":" + obj.toString());
}
return sb.toString();
}
}; } @Bean
public RedisConnectionFactory redisConnectionFactory() {
return new JedisConnectionFactory();
} @Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
} @Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
return new RedisCacheManager(redisTemplate);
}
}

使用例子

package cn.xiaojf.today.sys.dao.impl;

import cn.xiaojf.today.base.dao.impl.BaseDaoImpl;
import cn.xiaojf.today.base.model.PageInfo;
import cn.xiaojf.today.base.util.Asserts;
import cn.xiaojf.today.sys.dao.SysUserDao;
import cn.xiaojf.today.sys.repository.SysUserRepository;
import cn.xiaojf.today.sys.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Repository; import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set; /**
* 用户数据访问接口实现类
* @author xiaojf 2017/2/9 17:40.
*/
@Repository
@CacheConfig(cacheNames = {"sysUserCache"})
public class SysUserDaoImpl extends BaseDaoImpl<SysUser> implements SysUserDao {
@Autowired
private SysUserRepository userRepository; public SysUserDaoImpl() {
super(SysUserRepository.class);
} @Override
// @Cacheable(key = "#username")
public SysUser getByUsername(String username) {
Asserts.notNull(username,"用户名不能为空");
SysUser user = userRepository.findFirstByUsername(username);
return user;
}
}

spring cache的其他使用方式很简单,可以直接看官网或者百度例子

springboot redis 缓存对象的更多相关文章

  1. SpringBoot Redis缓存 @Cacheable、@CacheEvict、@CachePut

    文章来源 https://blog.csdn.net/u010588262/article/details/81003493 1. pom.xml <dependency> <gro ...

  2. springboot Redis 缓存

    1,先整合 redis 和 mybatis 步骤一: springboot 整合 redis 步骤二: springboot 整合 mybatis 2,启动类添加 @EnableCaching 注解, ...

  3. 微服务-Springboot+Redis缓存管理接口代码实现

    废话少说,上代码,结合代码讲解: 一.创建maven工程:导入依赖: <packaging>war</packaging><!--修改jdk的版本--><pr ...

  4. springboot + redis缓存使用

    [参照资料] 1.spring boot 官网文档 2.https://www.cnblogs.com/gdpuzxs/p/7222309.html [项目结构] [pom.xml配置] <?x ...

  5. 从.Net到Java学习第七篇——SpringBoot Redis 缓存穿透

    从.Net到Java学习系列目录 场景描述:我们在项目中使用缓存通常都是先检查缓存中是否存在,如果存在直接返回缓存内容,如果不存在就直接查询数据库然后再缓存查询结果返回.这个时候如果我们查询的某一个数 ...

  6. redis 缓存对象、列表

    在spring boot环境下有个StringRedisTemplate对象,默认已经为我们配置好了,只需要自动注入过来就能用,但是使用它只能在Redis中存放字符串.具体操作如下: @RunWith ...

  7. springboot redis 缓存跨域丢失问题

    对于前后端分离的项目,在开发阶段经常会遇到跨域的问题. 1.首先,对于后台的处理方式,第一种是用 @CrossOrigin 注解,@crossorigin是后端SpringMVC框架(需4.2版本以上 ...

  8. Redis缓存 序列化对象存储乱码问题

    使用Redis缓存对象会出现下图现象: 键值对都是乱码形式. 解决以上问题: 如果是xml配置的 我们直接注入官方给定的keySerializer,valueSerializer,hashKeySer ...

  9. C# mvc 前端调用 redis 缓存的信息

    新手 这几天网上学习下redis ,自己总结下过程,怕之后忘记了,基本会用最简单的,有的还是不懂,先记下来,自己摸索的. 没有安装redis的先安装,教程:http://www.cnblogs.com ...

随机推荐

  1. matlab 2016a破解中文版安装教程

    之前电脑重装过,所以要重新安装一个matlab,在大三的时候学过matlab,信息老师给的安装包,但是不知道放哪里去了,记忆力不好,找了些网上的教程和下载地址,真的是坑,一些都是不行的,在这里记录下m ...

  2. 解决移动端click点击问题

    下载地址:https://github.com/ftlabs/fastclick 1,为什么移动端点击会有300ms的延迟呢? 从点击屏幕上的元素到触发元素的 click 事件,移动浏览器会有大约 3 ...

  3. mysql性能优化配置总结

    看了一些优化mysql运维的一些书籍,在此记录总结下:进入mysql客户端输入以下sql:1.连接设置 show variables like '%max_connection%'; show sta ...

  4. lua 运算符

    lua 运算符 算术运算符 操作符 描述 + 加 - 减 * 乘 / 除 % 求模 ^ 求幂 示例程序 local a, b = 1, 2 print(a + b) print(a - b) prin ...

  5. PMBOK 学习与实践分享视频

    本系列为自己在学习PMBOK时进行的总结与分享,每一节主要包括两部分: 对PMBOK本身的一个结构笔记和讲解. 对自己项目管理工作的一个总结和思考. PMBOK 学习与实践分享视频内容清单 人力资源管 ...

  6. JS基础——入门必备

    ·首先,来简单的说一下,JS是啥,JS是JavaScript的简写,是 基于浏览器的 基于对象的 事件驱动 脚本语言 ·那么JS有什么用呢?ta可以实现: 表单验证 添加动画效果 动态更改页面内容 A ...

  7. 蓝桥杯-隔行变色-java

    /* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...

  8. 推荐免费的svn空间

    可能大部分人跟我一样办公和其他环境的代码同步选择用github,但github免费用户的代码是公开的.如果想做商业项目,用免费的github账号就不合适了,这里推荐的免费svn是个不错的选择. 1.h ...

  9. linux 内核的futex

    futex是linux内核为用户空间实现锁等同步机制而设计的同步排队(队列queueing)服务.在futex.c的注释中,futex起源于"Fast Userspace Mutex&quo ...

  10. python基本运算

    环境:python3.x a,b = 60,164 一.算数运算符 操作符 描述 例子 + 加法 a+b = 224 - 减法 a-b = -104 * 乘法 a*b = 9840 / 除(保留小数位 ...