使用spring-data-redis操作redis
2014-01-23 10:52
746 查看
http://docs.spring.io/spring-data/data-redis/docs/1.1.0.RELEASE/reference/html/
一、maven
二、spring 配置:
添加spring格式头
配置bean:
三、例子
一、maven
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.1.0.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.1.0</version> </dependency>
二、spring 配置:
添加spring格式头
xmlns:p=http://www.springframework.org/schema/p
配置bean:
<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true" p:host-name="192.168.0.60" p:port="6379"/> <!-- redis template definition --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnFactory"> <!-- 使用string主要是key 在redis端用命令好读 不然默认的序列化没办法读 --> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> </bean>
三、例子
package com.vrv.im.service.impl.helper; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.ListOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import com.vrv.im.domain.IMCommentSubject; /** * @date 2013年12月26日 上午11:47:22 * @version V1.0 * @Description: 缓存新鲜事的最近两条评论,所有的新鲜事, * value如果默认序列化存储占用空间大,可以使用hashmap 速度快 占用空间小 redis端可读性强 * 默认的就是代码简洁 * * 缓存对象字段有点多,可以优化减少 */ @Component public class CommentInfoCacheHandler { @Autowired private RedisTemplate<String, String> template; private String commentInfoKey="commentInfo"; // inject the template as ListOperations @Resource(name="redisTemplate") private ListOperations<String,IMCommentSubject> commentInfoHash; /** * 获取缓存评论 * @param subjectCommentID * @return */ public List<IMCommentSubject> getCommentInfoCache(long subjectCommentID){ List<IMCommentSubject> list= commentInfoHash.range(commentInfoKey+"_"+subjectCommentID, 0, -1);//获取所有 if(list==null) list= new ArrayList<IMCommentSubject>(); return list; } /** * 添加缓存信息 * @param commentInfo */ public void addCommentInfoCache(IMCommentSubject commentInfo){ commentInfoHash.leftPush(commentInfoKey+"_"+commentInfo.getSubjectCommentID(), commentInfo); commentInfoHash.trim(commentInfoKey+"_"+commentInfo.getSubjectCommentID(), 0, 1);//保留2条 } /** * 删除评论主体缓存 * @param subjectCommentID */ public void deleteCommentInfoCache(long subjectCommentID){ template.delete(commentInfoKey+"_"+subjectCommentID);//所有都删除 } /** * 删除评论主体的某条评论 * @param subjectCommentID */ public void deleteCommentInfoCache(IMCommentSubject commentInfo){ commentInfoHash.remove(commentInfoKey+"_"+commentInfo.getSubjectCommentID(), 0, commentInfo);//相等的评论信息 } }
相关文章推荐
- 使用Spring-data-redis操作Redis的Sentinel
- 使用spring-data-redis进行对redis的操作,封装的一些操作方法
- Spring中使用RedisTemplate操作Redis(spring-data-redis)
- Spring中使用RedisTemplate操作Redis(spring-data-redis)
- 使用Spring-data进行Redis操作
- 使用Spring-data-redis操作Redis的Sentinel
- 使用spring-data-redis操作redis
- 使用spring-data-redis兼容redis单机和集群操作
- 使用Spring-data-redis操作Redis的Sentinel
- 使用spring-data-redis进行对redis的操作,封装的一些操作方法
- 使用spring-data-redis操作Redis的Sentinel
- 使用Spring-data-redis操作Redis的Sentinel
- 在spring Boot中使用Spring-data-jpa操作数据库
- spring data redis 操作redis
- 【Redis】Redis的基本安装及使用、Jedis的基本使用、spring-data-redis的集成、主从模式、哨兵模式
- spring-boot-starter-data-redis(spring cloud 操作redis) RedisTemplate
- Spring-data-redis操作redis知识总结
- Spring整合redis使用Java操作redis
- Spring Data操作Redis时,发现key值出现 \xac\xed\x00\x05t\x00\tb
- 使用Java(Springboot)操作Redis