使用spring-data-redis操作redis
来源:互联网 发布:avmo.pw域名更新 编辑:程序博客网 时间:2024/05/19 07:10
http://docs.spring.io/spring-data/data-redis/docs/1.1.0.RELEASE/reference/html/
一、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端可读性强 * 默认的就是代码简洁 * * 缓存对象字段有点多,可以优化减少 */@Componentpublic 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);//相等的评论信息 } }
0 0
- 使用spring-data-redis操作redis
- spring data redis 操作redis
- spring data redis 操作redis
- spring-data-redis 操作
- 使用Spring-data进行Redis操作
- 使用Spring-data-redis操作Redis的Sentinel
- 使用spring-data-redis操作Redis的Sentinel
- 使用Spring-data-redis操作Redis的Sentinel
- 使用Spring Data Redis操作Redis(一) 很全面
- 使用Spring Data Redis操作Redis(一) 很全面
- spring-data-redis 使用
- spring-data-redis 使用
- Spring Data Redis 使用
- spring data redis使用
- Spring+redis,spring-data-redis使用
- spring-data-redis操作redis集群
- spring-data-redis连接操作redis
- Spring-data-redis操作Redis的Sentinel
- 杭电2013
- redis命令行
- ios app crash报告分析
- Spring3 MVC详解一
- EBS Form开发中LOV实现实例(3)
- 使用spring-data-redis操作redis
- java常用算法
- jQuery——jQuery判断文本内容:empty/contains("文本内容")/has("CSS选择器")/parent("CSS选择器")
- fcitx日语输入法添加
- Spring3 MVC详解二
- 10大流行的 Metro UI 风格的 Bootstrap 主题和模板
- java中8种原生数据类型
- numtoyminterval函数的用法
- DBCP,C3P0,Tomcat_JDBC druidDatasource 性能及稳定性测试