(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. HNOI2018滚粗记

    day 0 最近发现机房的人都有些焦虑(除了一些神犇)自己也被影响地紧张起来 唉,不知道是不是一种好的心态,紧张是必然的... 随便打了点板子(\(FFT,SA,LCT\)) 很棒一个都没考 day ...

  2. Java新AIO/NIO2:AsynchronousServerSocketChannel和AsynchronousSocketChannel简单服务器-客户端

    Java新AIO/NIO2:AsynchronousServerSocketChannel和AsynchronousSocketChannel简单服务器-客户端用AsynchronousServerS ...

  3. 学习Spring Boot:(二十一)使用 EhCache 实现数据缓存

    前言 当多次查询数据库影响到系统性能的时候,可以考虑使用缓存,来解决数据访问新能的问题. SpringBoot 已经为我们提供了自动配置多个 CacheManager 的实现,只要去实现使用它就可以了 ...

  4. 前端学习 -- Html&Css -- ie6 png 背景问题

    在IE6中对图片格式png24支持度不高,如果使用的图片格式是png24,则会导致透明效果无法正常显示 解决方法: 1.可以使用png8来代替png24,即可解决问题,但是使用png8代替png24以 ...

  5. DIVCNT2&&3 - Counting Divisors

    DIVCNT2 - Counting Divisors (square) DIVCNT3 - Counting Divisors (cube) 杜教筛 [学习笔记]杜教筛 (其实不算是杜教筛,类似杜教 ...

  6. 面向对象——类的内置attr(三十三)

    class Foo: x=1 def __init__(self,y): self.y=y def __getattr__(self, item): print('----> from geta ...

  7. springboot配置多环境

    https://www.cnblogs.com/jason0529/p/6567373.html   Spring的profiles机制,是应对多环境下面的一个解决方案,比较常见的是开发和测试环境的配 ...

  8. Makefile ------ .PHONY的作用

    看下面的例子 Makefile文件 .PHONY: cleanclean: rm *.o 当Makefile文件所在目录有文件名为clean的文件,命令行“.PHONY: clean”又没添加的话,执 ...

  9. java.sql.SQLSyntaxErrorException: ORA-00911: 无效字符

    java.sql.SQLSyntaxErrorException: ORA-: 无效字符 at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer. ...

  10. C#怎么调用百度地图Web API

    直接上代码: public ActionResult FindMileage() { string s; HttpWebRequest req = (HttpWebRequest)HttpWebReq ...