(1)pom添加依赖项

     <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>

(2)设置相应的bean,添加@EnableCaching 注解启用缓存功能

 package cn.coreqi.config;

 import cn.coreqi.entities.User;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.cache.support.CompositeCacheManager;
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 org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer; import java.util.ArrayList;
import java.util.List; @Configuration
@EnableCaching //启用缓存 <cache:annotation-driven />
public class RedisConfig {
// @Bean
// public RedisConnectionFactory redis(){
// RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
// redisStandaloneConfiguration.setHostName("192.168.205.128");
// redisStandaloneConfiguration.setPort(6379);
// redisStandaloneConfiguration.setDatabase(0);
// //redisStandaloneConfiguration.setPassword(RedisPassword.of("123456"));
//
// JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();
// jedisClientConfiguration.connectTimeout(Duration.ofMillis(6000));// connection timeout
//
// JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration.build());
// return factory;
// } //Redis连接工厂
@Bean
public RedisConnectionFactory redis(){
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName("192.168.205.128");
factory.setPort(6379);
return factory;
} //RedisTemplate
@Bean
public RedisTemplate<String, User> redisTemplate(RedisConnectionFactory redis){
RedisTemplate<String, User> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redis);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<User>(User.class));
return redisTemplate;
} // @Bean
// public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redis){
// return new StringRedisTemplate(redis);
// } //Redis缓存管理器
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate){
return new RedisCacheManager(redisTemplate);
} //注册多个缓存管理器(迭代)
// @Bean
// public CacheManager cacheManager(RedisTemplate redisTemplate){
// CompositeCacheManager cacheManager = new CompositeCacheManager();
// List<CacheManager> managers = new ArrayList<>();
// managers.add(new ConcurrentMapCacheManager());
// managers.add(new RedisCacheManager(redisTemplate));
// cacheManager.setCacheManagers(managers);
// return cacheManager;
// }
}

(3)在dao相应的方法上添加缓存相关注解(可以在dao接口或dao实现类上添加)

此处仅作实例代码。

 package cn.coreqi.dao.redis;

 import cn.coreqi.entities.User;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository; import java.util.List; @Repository
public class UserRedis {
@Cacheable("Users")
public List<User> getAll(){
return null;
}
@Cacheable("Users")
public User getById(int Id){
return null;
}
@CachePut(value = "Users",key = "#result.Id")
public User modify(User user){
return null;
}
@CacheEvict("Users")
public void delById(int Id){
}
}

SpringMVC使用Redis作为缓存提供者的更多相关文章

  1. spring+springmvc+mybatis+redis实现缓存

    先搭建好redis环境 需要的jar如下: jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:330 ...

  2. SpringMVC通过Redis实现缓存主页

    这里说的缓存只是为了提供一些动态的界面没办法作静态化的界面来减少数据库的访问压力,如果能够做静态化的话的还是采用nginx来做界面的静态化,这样可以承受高并发的访问能力. 好了,废话少说直接看实现代码 ...

  3. SpringBoot集成Redis实现缓存处理(Spring AOP实现)

    第一章 需求分析 计划在Team的开源项目里加入Redis实现缓存处理,因为业务功能已经实现了一部分,通过写Redis工具类,然后引用,改动量较大,而且不可以实现解耦合,所以想到了Spring框架的A ...

  4. mybatis-自定义缓存-redis二级缓存

    在mybatis一级缓存二级缓存中已经介绍过了二级缓存的大致原理.下面我们用redis来实现一下二级缓存.环境是springmvc+mybatis+redis 步骤一.引入redis相关的maven依 ...

  5. springmvc+mybatis+redis实现查询插入操作

    最近在学习redis,虽然现在还不是很熟练.不过可以进行简单的框架整合开发. IDE:我使用的是IDEA.springmvc+spring+mybatis的整合这个我就不多说了,下面我们先进行这块的整 ...

  6. springboot+redis实现缓存数据

    在当前互联网环境下,缓存随处可见,利用缓存可以很好的提升系统性能,特别是对于查询操作,可以有效的减少数据库压力,Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存 ...

  7. 如何玩转最新的项目的搭配springmvc+mybatis+Redis+Nginx+tomcat+mysql

    上一次完成nginx+tomcat组合搭配,今天我们就说说,这几个软件在项目中充当的角色: 要想完成这几个软件的组合,我们必须知道和熟悉应用这个框架, 一: Nginx:在项目中大多数作为反向代理服务 ...

  8. c#实例化继承类,必须对被继承类的程序集做引用 .net core Redis分布式缓存客户端实现逻辑分析及示例demo 数据库笔记之索引和事务 centos 7下安装python 3.6笔记 你大波哥~ C#开源框架(转载) JSON C# Class Generator ---由json字符串生成C#实体类的工具

    c#实例化继承类,必须对被继承类的程序集做引用   0x00 问题 类型“Model.NewModel”在未被引用的程序集中定义.必须添加对程序集“Model, Version=1.0.0.0, Cu ...

  9. Springboot使用Shiro-整合Redis作为缓存 解决定时刷新问题

    说在前面 (原文链接: https://blog.csdn.net/qq_34021712/article/details/80774649)本来的整合过程是顺着博客的顺序来的,越往下,集成的越多,由 ...

随机推荐

  1. importlib 模块

    根据字符串的模块名实现动态导入模块的库 目录结构 ├── aaa.py ├── bbb.py └── mypackage ├── __init__.py └── xxx.py bbb.py impor ...

  2. Java OOM 常见情况

    Java OOM 常见情况 原文:https://blog.csdn.net/qq_42447950/article/details/81435080 1)什么是OOM?  OOM,全称“Out Of ...

  3. 在Java Web程序中使用监听器可以通过以下两种方法

    之前学习了很多涉及servlet的内容,本小结我们说一下监听器,说起监听器,编过桌面程序和手机App的都不陌生,常见的套路都是拖一个控件,然后给它绑定一个监听器,即可以对该对象的事件进行监听以便发生响 ...

  4. 洛谷 P4074 [WC2013]糖果公园 解题报告

    P4074 [WC2013]糖果公园 糖果公园 树上待修莫队 注意一个思想,dfn序处理链的方法,必须可以根据类似异或的东西,然后根据lca分两种情况讨论 注意细节 Code: #include &l ...

  5. httpd.yml实例

    httpd.ymlapiVersion: apps/v1beta1kind: Deploymentmetadata: name: httpdspec: replicas: 4 template: me ...

  6. change username on ubuntu.

    Below tutorial will show you how to change username in ubuntu 12.04 precise.First,we need login as r ...

  7. JS循环语句!

    <1> for(1.初始值(初始值只有一次):2.判断条件:4.状态改变){ 3.执行语句: //如果判断条件为true,则进入死循环:不设执行语句浏览器会未响应: } <2> ...

  8. Redis我想入门——启动

    nosql数据库出现到现在很多年了.笔者一直从事C/S模式的上开发.所以相对而言笔者只是听过却从来不知道他是什么东西.时代在变化,当年所有业务都放在一个War包的时代已经不在了.微服务已经成为了世界主 ...

  9. 安装架设Apache+MySQL+PHP网站环境

    转载自 http://blog.sina.com.cn/s/blog_c02ed6590101d2sl.html 并进行了个人编辑整理 一.安装 MySQL 首先来进行 MySQL 的安装.打开超级终 ...

  10. banner轮播无缝滚动 jq代码

    HTML: <div class="box"> <ul> <li>11111</li> <li>22222</li ...