Redis缓存
来源:互联网 发布:程序员中级考试 答案 编辑:程序博客网 时间:2024/06/05 04:04
import redis.clients.jedis.ShardedJedis;
public interface RedisService {
/**
* 执行set
* @return
*/
public <T> T excute(Function<ShardedJedis, T> fun);
/**
* 执行set
*/
public String set(final String key,final String value);
/**
* 执行get
*/
public String get(final String key);
/**
* 设置生存时间
*/
public Long expire(final String key,final Integer seconds);
/**
* 设置key和value并设置生存时间
*/
public Long setExpire(final String key,final String value,final Integer seconds);
/**
* 删除键
*/
public Long del(final String key);
}
public interface Function<E,T> {
public T callback(E e);
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qiantufax.manage.service.Function;
import com.qiantufax.manage.service.RedisService;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
@Service
public class RedisServiceImpl implements RedisService{
@Autowired(required = false)//使用到时才注册该对象
private ShardedJedisPool shardedJedisPool;
/**执行set*/
public <T> T excute(Function<ShardedJedis, T> fun){
ShardedJedis shardedJedis = null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
return fun.callback(shardedJedis);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
return null;
}
/**执行set*/
public String set(final String key,final String value){
return this.excute(new Function<ShardedJedis, String>() {
@Override
public String callback(ShardedJedis e) {
return e.set(key, value);
}
});
}
/**执行get*/
public String get(final String key){
return this.excute(new Function<ShardedJedis, String>() {
@Override
public String callback(ShardedJedis e) {
return e.get(key);
}
});
}
/**设置生存时间*/
public Long expire(final String key,final Integer seconds){
return this.excute(new Function<ShardedJedis, Long>(){
@Override
public Long callback(ShardedJedis e) {
Long expire = e.expire(key, seconds);
return expire;
}
});
}
/**设置key和value并设置生存时间*/
public Long setExpire(final String key,final String value,final Integer seconds){
return this.excute(new Function<ShardedJedis, Long>(){
@Override
public Long callback(ShardedJedis e) {
e.set(key, value);
return e.expire(key, seconds);
}
});
}
/**删除键*/
public Long del(final String key){
return this.excute(new Function<ShardedJedis, Long>(){
@Override
public Long callback(ShardedJedis e) {
return e.del(key);
}
});
}
}
public interface RedisService {
/**
* 执行set
* @return
*/
public <T> T excute(Function<ShardedJedis, T> fun);
/**
* 执行set
*/
public String set(final String key,final String value);
/**
* 执行get
*/
public String get(final String key);
/**
* 设置生存时间
*/
public Long expire(final String key,final Integer seconds);
/**
* 设置key和value并设置生存时间
*/
public Long setExpire(final String key,final String value,final Integer seconds);
/**
* 删除键
*/
public Long del(final String key);
}
public interface Function<E,T> {
public T callback(E e);
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qiantufax.manage.service.Function;
import com.qiantufax.manage.service.RedisService;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
@Service
public class RedisServiceImpl implements RedisService{
@Autowired(required = false)//使用到时才注册该对象
private ShardedJedisPool shardedJedisPool;
/**执行set*/
public <T> T excute(Function<ShardedJedis, T> fun){
ShardedJedis shardedJedis = null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
return fun.callback(shardedJedis);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
return null;
}
/**执行set*/
public String set(final String key,final String value){
return this.excute(new Function<ShardedJedis, String>() {
@Override
public String callback(ShardedJedis e) {
return e.set(key, value);
}
});
}
/**执行get*/
public String get(final String key){
return this.excute(new Function<ShardedJedis, String>() {
@Override
public String callback(ShardedJedis e) {
return e.get(key);
}
});
}
/**设置生存时间*/
public Long expire(final String key,final Integer seconds){
return this.excute(new Function<ShardedJedis, Long>(){
@Override
public Long callback(ShardedJedis e) {
Long expire = e.expire(key, seconds);
return expire;
}
});
}
/**设置key和value并设置生存时间*/
public Long setExpire(final String key,final String value,final Integer seconds){
return this.excute(new Function<ShardedJedis, Long>(){
@Override
public Long callback(ShardedJedis e) {
e.set(key, value);
return e.expire(key, seconds);
}
});
}
/**删除键*/
public Long del(final String key){
return this.excute(new Function<ShardedJedis, Long>(){
@Override
public Long callback(ShardedJedis e) {
return e.del(key);
}
});
}
}
0 0
- Redis缓存
- redis缓存
- Redis缓存
- Redis缓存
- redis缓存
- Redis缓存
- redis 缓存
- 缓存-redis
- redis缓存
- redis 缓存
- redis缓存
- REDIS缓存
- redis缓存
- Redis缓存
- redis缓存
- Redis缓存
- redis缓存
- redis缓存
- 系统编程二
- 【C++后台开发面试】Linux系统相关
- pandoc md 文件转化pdf(中文字体)
- 8——合并表记录
- spring @Transactional 注解学习
- Redis缓存
- iOS开发学习专题-基础知识(三)NSDictionary字典NSMutableDictionary可变字典NSDictionary字典和json转换详细使用方式
- GSON解析Json格式数据
- JAVA设计模式------观察者模式
- oracle字符串转成date类型
- iOS addChildViewController方法
- Redis起步安装及基本命令--JAVA(系列文章一)
- 测试的划分
- 文章标题