您的位置:首页 > 数据库 > Redis

使用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

<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);//相等的评论信息
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: