package com.mkfree.redis.test;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import redis.clients.jedis.Jedis;
/**
* 封装redis 缓存服务器服务接口
*
* @author hk http://blog.csdn.net/sunny243788557/article/details/34431357
* 2012-12-16 上午3:09:18
*/
public class RedisService {
/**
* 通过key删除(字节)
*
* @param key
*/
public void del(byte[] key) {
this.getJedis().del(key);
}
/**
* 通过key删除
*
* @param key
*/
public void del(String key) {
this.getJedis().del(key);
}
/**
* 添加key value 并且设置存活时间(byte)
*
* @param key
* @param value
* @param liveTime
*/
public void set(byte[] key, byte[] value, int liveTime) {
this.set(key, value);
this.getJedis().expire(key, liveTime);
}
/**
* 添加key value 并且设置存活时间
*
* @param key
* @param value
* @param liveTime
*/
public void set(String key, String value, int liveTime) {
this.set(key, value);
this.getJedis().expire(key, liveTime);
}
/**
* 添加key value
*
* @param key
* @param value
*/
public void set(String key, String value) {
this.getJedis().set(key, value);
}
/**
* 添加key value (字节)(序列化)
*
* @param key
* @param value
*/
public void set(byte[] key, byte[] value) {
this.getJedis().set(key, value);
}
/**
* 获取redis value (String)
*
* @param key
* @return
*/
public String get(String key) {
String value = this.getJedis().get(key);
return value;
}
/**
* 获取redis value (byte [] )(反序列化)
*
* @param key
* @return
*/
public byte[] get(byte[] key) {
return this.getJedis().get(key);
}
/**
* 通过正则匹配keys
*
* @param pattern
* @return
*/
public Set<String> keys(String pattern) {
return this.getJedis().keys(pattern);
}
/**
* 检查key是否已经存在
*
* @param key
* @return
*/
public boolean exists(String key) {
return this.getJedis().exists(key);
}
/**
* 清空redis 所有数据
*
* @return
*/
public String flushDB() {
return this.getJedis().flushDB();
}
/**
* 查看redis里有多少数据
*/
public long dbSize() {
return this.getJedis().dbSize();
}
/**
* 检查是否连接成功
*
* @return
*/
public String ping() {
return this.getJedis().ping();
}
/**
* 获取一个jedis 客户端
*
* @return
*/
private Jedis getJedis() {
if (jedis == null) {
return jedisConnectionFactory.getShardInfo().createResource();
}
return jedis;
}
private RedisService() {
}
// 操作redis客户端
private static Jedis jedis;
@Autowired
@Qualifier("jedisConnectionFactory")
private JedisConnectionFactory jedisConnectionFactory;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
SpringRedis.zip (9个子文件)
SpringRedis
bin
com
mkfree
redis
test
RedisService.class 3KB
TestRedis.class 2KB
spring-context.xml 1KB
.settings
org.eclipse.jdt.core.prefs 598B
src
com
mkfree
redis
test
RedisService.java 3KB
TestRedis.java 2KB
spring-context.xml 1KB
.project 387B
.classpath 2KB
共 9 条
- 1
资源评论
- doufuplus2018-07-24别被骗了,里面就简简单单两个Java类,一个像样的Spring整合xml都没,简直浪费表情
无心流泪
- 粉丝: 279
- 资源: 56
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功