下载spring-data-redis,gav如下:
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>1.0.1.RELEASE</version>
- <exclusions>
- <exclusion>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- </exclusion>
- <exclusion>
- <groupId>org.slf4j</groupId>
- <artifactId>jcl-over-slf4j</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
其中exclusion了两个包,原因是与项目里其它包冲突。
bean配置如下,可在web.xml里配置加载bean文件:
- <bean id="redisCacheManager" class="com.cr.common.cache.base.RedisCacheManger">
- <property name="pool" ref="shardedJedisPool"/>
- </bean>
- <!-- jedis 连接池配置-->
- <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
- <property name="maxActive" value="${redis.pool.maxActive}" />
- <property name="maxIdle" value="${redis.pool.maxIdle}" />
- <property name="maxWait" value="${redis.pool.maxWait}" />
- <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
- </bean>
- <!-- jedis 多个服务器配置-->
- <bean id="jedisShardInfo1" class="redis.clients.jedis.JedisShardInfo">
- <constructor-arg index="0" value="${redis2.ip}" />
- <constructor-arg index="1" value="${redis.port}" type="int" />
- </bean>
- <bean id="jedisShardInfo2" class="redis.clients.jedis.JedisShardInfo">
- <constructor-arg index="0" value="${redis.ip}" />
- <constructor-arg index="1" value="${redis.port}" type="int" />
- </bean>
- <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
- <constructor-arg index="0" ref="jedisPoolConfig" />
- <constructor-arg index="1">
- <list>
- <ref bean="jedisShardInfo1" />
- <ref bean="jedisShardInfo2"/>
- </list>
- </constructor-arg>
- </bean>
- <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
- >
- <property name="hostName" value="${redis.ip}" />
- <property name="port" value="${redis.port}" />
- <property name="poolConfig" ref="jedisPoolConfig" />
- <!--<property name="shardInfo" ref="shardedJedisPool"></property>-->
- </bean>
- <context:property-placeholder location="/WEB-INF/spring/SystemContext.properties"/>
- <context:component-scan base-package="org.springframework.data.redis.samples"/>
属性文件内容如下:
- redis.ip=192.168.1.110
- redis2.ip=192.168.1.112
- #Port
- redis.port=6379
- #最大分配的对象数
- redis.pool.maxActive=1024
- #最大能够保持idel状态的对象数
- redis.pool.maxIdle=200
- #当池内没有返回对象时,最大等待时间
- redis.pool.maxWait=1000
- #当调用borrow Object方法时,是否进行有效性检查
- redis.pool.testOnBorrow=true
- #当调用return Object方法时,是否进行有效性检查
- redis.pool.testOnReturn=true
缓存管理接口:
- public interface RedisCache {
- public <T> T getRedisCacheInfo(String key);
- public <T> boolean setRedisCacheInfo(String key, T value);
- }
缓存管理实现:
- public class RedisCacheManger implements RedisCache {
- private ShardedJedisPool pool ;
- private Logger log = Logger.getLogger(RedisCacheManger.class);
- public ShardedJedisPool getPool() {
- return pool;
- }
- public void setPool(ShardedJedisPool pool) {
- this.pool = pool;
- }
- public <T> T getRedisCacheInfo(String key) {
- try {
- log.info("get from redisCache :"+key);
- System.out.println("get from rediscache");
- ShardedJedis jedis = pool.getResource();
- pool.returnResource(jedis);
- return (T)jedis.get(key);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public <T> boolean setRedisCacheInfo(String key, T value) {
- try {
- log.info("add to redisCache :"+key);
- System.out.println("add to rediscache");
- ShardedJedis jedis = pool.getResource();
- jedis.set(key, (String)value);
- pool.returnResource(jedis);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
- public static void main(String[] args) {
- new RedisCacheManger().setRedisCacheInfo("12345", "asdfg");
- }
- }
缓存切面注解:
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.METHOD)
- public @interface NeedRedisCached {}
缓存切面处理类:
- @Aspect
- public class RedisCacheAspect implements Ordered {
- private static Logger log = Logger.getLogger(RedisCacheAspect.class);
- private RedisCache redisCacheManager;
- private int orderValue = 3;
- public RedisCache getRedisCacheManager() {
- return redisCacheManager;
- }
- public void setRedisCacheManager(RedisCache redisCacheManager) {
- this.redisCacheManager = redisCacheManager;
- }
- @Pointcut("@annotation(com.jd.bi.odp.common.cache.core.NeedRedisCached)")
- public void needRedisCached() {
- }
- @Around("needRedisCached() && args(filter,..)")
- public Object aroundInvoke(ProceedingJoinPoint pjp, QueryFilter filter) throws Throwable {
- log.info("enter aroundInvoke!!!");
- if (filter.getValue() == null) {
- return null;
- }
- boolean cacheEnabled = CommonUtil.parseBoolean(HBaseConfig.getProperty("redisCache.enabled"), false);
- if (cacheEnabled) {
- String md5key = MD5Util.getMD5(filter.getValue().toString());
- Object value = redisCacheManager.getRedisCacheInfo(md5key);
- boolean flag = false;
- if (null != value) {
- JSONObject json = new JSONObject(value.toString());
- return json;
- } else if ("null".equals(value)) {
- return null;
- } else { //执行hbase查询
- value = pjp.proceed();
- if(null!=value){//此处根据业务逻辑判断不缓存的条件
- }
- else{
- flag = redisCacheManager.setRedisCacheInfo(md5key, value.toString());
- if(flag)
- log.info("add a cache success by key: "+md5key);
- else
- log.warn("add a cache failure by key: "+md5key);
- }
- }
- return value;
- }
- } else {// 执行hbase查询
- return pjp.proceed();
- }
- }
- @Override
- public int getOrder() {
- return orderValue;
- }
- public int getOrderValue() {
- return orderValue;
- }
- public void setOrderValue(int orderValue) {
- this.orderValue = orderValue;
- }
- }
缓存存在直接返回,不存在的话执行数据源查询,此处是hbase查询,并设置缓存。
切面配置:
- <!-- redis缓存运行切面 -->
- <bean id="RedisCacheAspect"
- class="com.cr.common.cache.core.RedisCacheAspect">
- <property name="orderValue" value="3" />
- <property name="redisCacheManager" ref="redisCacheManager"/>
- </bean>
- <!-- 切面申明配置-->
- <aop:aspectj-autoproxy>
- <aop:include name="RedisCacheAspect" />
- </aop:aspectj-autoproxy>
此时,前端web页面用户的访问触发的action如果满足条件,则会进入切面方法处理,完成redis缓存的使用。
http://blog.csdn.net/cuirong1986/article/details/8213159?utm_source=tuicool&utm_medium=referral
相关推荐
SpringCloud整合Redis缓存;版本:SpringCloud :Dalston.SR4;SpringBoot:1.5.14.RELEASE;Redis:3.2.0;Maven :3.5.3.代码下载下来即可用
Spring 3.0虽然相对较老,但在当时是广泛使用的版本,因此了解其与Redis的集成方式对维护旧项目或理解历史背景很有帮助。 首先,我们需要引入Redis的相关jar包。在"redis必须jar"中,通常包括以下组件: 1. `jedis....
总的来说,集成Redis与Spring Cache可以简化缓存的管理和使用,通过合理配置和使用,能够有效地提升系统的响应速度和效率。在实际开发中,应根据业务需求调整缓存策略,例如设置合适的缓存过期时间,以及处理缓存和...
这样,我们便完成了Redis与Spring的集成,能够方便地使用Java代码对Redis进行各种操作。记住,合理利用Redis的特性(如批量操作、订阅发布等)可以显著提高应用的性能和响应速度。在实际项目中,根据业务需求进行...
将Redis与Spring集成,可以充分利用Redis的强大功能,同时利用Spring的便捷性。 集成Redis与Spring主要通过Spring Data Redis模块实现。Spring Data Redis提供了对Redis的高级抽象,包括模板类、Repository支持以及...
将Redis与Spring整合,可以充分利用Redis的高速存储优势,提升应用程序的性能。本文将深入探讨如何进行Redis与Spring的整合,并详细解析整合过程中的关键步骤。 首先,我们要理解Spring对数据源的支持。Spring提供...
我们将深入探讨如何在Spring框架中集成Redis,并通过注解方式实现数据的存取。 首先,Redis作为缓存系统,可以大大提高应用程序的响应速度,减少对数据库的直接访问,从而降低系统负载。在Spring中整合Redis,我们...
spring-data-redis-1.6.2.RELEASE.jar spring与redis集成包 commons-pool2-2.4.2.jar 与redis连接池 spring-data-commons-2.0.0.RC2.jar spring数据包 redis-context.xml redis和spring配置,需要引用到自己项目的...
【Redis与Spring集成】 Redis,一个高性能的键值对存储系统,常被用作数据库、缓存和消息中间件。其高效性能得益于内存存储和基于键值的数据结构。Spring Data Redis是Spring框架的一个模块,目的是简化Redis在Java...
spring集成redis,spring mvc实现session共享以及redis排行榜春季会议Redis这一系列项目,将会有多个spring相关的简单的例子,包括session-with-redis、spring整合redis等1、在springmvc项目session存储到redis中...
在IT行业中,Spring Boot是一个非常流行的微服务框架,它简化了Spring应用的初始搭建...通过这样的集成,我们可以利用Spring Boot的便利性,结合Redis的高速缓存能力与MyBatis的数据库操作,构建出高效稳定的Web应用。
`redis-spring-boot-starter`是Spring Boot生态中的一个启动器(Starter),它简化了Redis与Spring Boot集成的过程,使得开发者无需手动配置复杂的Redis连接参数,就能快速地在应用中启用Redis服务。这个启动器通常...
在这个项目中,Spring负责整体架构的管理,包括依赖注入、AOP(面向切面编程)以及与Shiro和Redis的集成。 **Shiro与Spring的集成** 将Shiro与Spring结合,可以利用Spring的IOC容器管理Shiro的组件,如Realm(领域...
本文将详细介绍如何在Spring应用中集成Redis,以及如何操作String、list、set、map四种基本数据类型。 一、整合步骤 1. **环境配置**:首先确保本地安装了Redis服务器,并且能够在应用中访问。同时,添加Redis的...
spring和redis集成有很多方式,看到网上很多都是使用redistemplate自己去做redis 的一些操作,但是对于我们开发来说,肯定是使用越方便越好,于是乎就有了spring的对redis或者memcahe这些换成框架的封装,只需要引入...
首先,集成SpringBoot与Redis的关键在于添加依赖。在`pom.xml`或`build.gradle`文件中,我们需要引入Spring Data Redis的依赖,这将提供对Redis操作的支持。例如,在Maven项目中,可以在`pom.xml`中添加以下代码: ...
在本项目中,"SpringMvc集成Redis项目完整示例" 提供了一个全面的教程,教你如何将Spring MVC框架与Redis缓存系统相结合。这个示例包括了Web应用程序的实例以及独立的Java测试案例,无需启动Web服务器即可进行测试。...
Redis缓存+Spring的集成示例 Redis缓存+Spring的集成示例Redis缓存+Spring的集成示例 Redis缓存+Spring的集成示例
标题中的“Spring-session2整合spring5+redis”指的是在Spring框架的第五个主要版本(Spring 5)中,集成Spring Session 2与Redis数据库来管理Web应用的会话(Session)。Spring Session是一个开源项目,旨在提供一...
通过Action类和拦截器,可以实现与Spring的无缝集成,同时也可以利用Spring的缓存功能,将频繁访问的数据存储在Redis中。 3. **Hibernate框架**:Hibernate是Java领域的一个对象关系映射(ORM)框架,它简化了数据库...