package com.deshengzheng.jedis;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands.DistanceUnit;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoLocation;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoRadiusCommandArgs;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
/**
*
* @ClassName:RedisService
* @Description:redis操作Dao:依赖spring-data-redis1.8.3,jedis2.9.0
* @Author:chenchuanliang
* @Date:2017年5月18日下午1:03:03
* @version:1.0.0
*/
public class RedisService {
/**
* 日志记录
*/
private static final Logger logger = LoggerFactory.getLogger(RedisService.class);
public RedisTemplate<String, String> redisTemplate;
public RedisTemplate<String, String> getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
this.redisTemplate = redisTemplate;
}
/**
* 前缀
*/
public static final String KEY_PREFIX_VALUE = "yb:value:";
public static final String KEY_PREFIX_SET = "yb:set:";
public static final String KEY_PREFIX_LIST = "yb:list:";
public static final String KEY_PREFIX_HASH = "yb:hash:";
public static final String KEY_PREFIX_GEO = "yb:geo:";
/**
*
* @MethodName:cacheValue
* @param k
* @param v
* @param time(单位秒) <=0 不过期
* @return
* @ReturnType:boolean
* @Description:缓存value操作
* @Creator:chenchuanliang
* @CreateTime:2017年5月18日上午11:24:56
* @Modifier:
* @ModifyTime:
*/
public boolean cacheValue(String k, String v, long time) {
String key = KEY_PREFIX_VALUE + k;
try {
ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
valueOps.set(key, v);
if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS);
return true;
} catch (Throwable t) {
logger.error("缓存[" + key + "]失败, value[" + v + "]", t);
}
return false;
}
/**
*
* @MethodName:cacheValue
* @param k
* @param v
* @return
* @ReturnType:boolean
* @Description:缓存value操作
* @Creator:chenchuanliang
* @CreateTime:2017年5月18日上午11:24:43
* @Modifier:
* @ModifyTime:
*/
public boolean cacheValue(String k, String v) {
return cacheValue(k, v, -1);
}
/**
*
* @MethodName:getValue
* @param k
* @return
* @ReturnType:String
* @Description:获取缓存
* @Creator:chenchuanliang
* @CreateTime:2017年5月18日上午11:21:37
* @Modifier:
* @ModifyTime:
*/
public String getValue(String k) {
String key = KEY_PREFIX_VALUE + k;
try {
ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
return valueOps.get(key);
} catch (Throwable t) {
logger.error("获取缓存失败key[" + key + ", error[" + t + "]");
}
return null;
}
/**
*
* @MethodName:cacheSet
* @param k
* @param v
* @param time
* @return
* @ReturnType:boolean
* @Description:缓存set操作
* @Creator:chenchuanliang
* @CreateTime:2017年5月18日上午11:20:00
* @Modifier:
* @ModifyTime:
*/
public boolean cacheSet(String k, String v, long time) {
String key = KEY_PREFIX_SET + k;
try {
SetOperations<String, String> valueOps = redisTemplate.opsForSet();
valueOps.add(key, v);
if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS);
} catch (Throwable t) {
t.printStackTrace();
logger.error("缓存[" + key + "]失败, value[" + v + "]", t);
}
return true;
}
/**
*
* @MethodName:cacheSet
* @param k
* @param v
* @return
* @ReturnType:boolean
* @Description:缓存set
* @Creator:chenchuanliang
* @CreateTime:2017年5月18日上午11:19:00
* @Modifier:
* @ModifyTime:
*/
public boolean cacheSet(String k, String v) {
return cacheSet(k, v, -1);
}
/**
*
* @MethodName:cacheSet
* @param k
* @param v
* @param time
* @return
* @ReturnType:boolean
* @Description:缓存set
* @Creator:chenchuanliang
* @CreateTime:2017年5月18日上午11:18:48
* @Modifier:
* @ModifyTime:
*/
public boolean cacheSet(String k, Set<String> v, long time) {
String key = KEY_PREFIX_SET + k;
try {
SetOperations<String, String> setOps = redisTemplate.opsForSet();
setOps.add(key, v.toArray(new String[v.size()]));
if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS);
return true;
} catch (Throwable t) {
logger.error("缓存[" + key + "]失败, value[" + v + "]", t);
}
return false;
}
/**
*
* @MethodName:cacheSet
* @param k
* @param v
* @return
* @ReturnType:boolean
* @Description:缓存set
* @Creator:chenchuanliang
* @CreateTime:2017年5月18日上午11:18:34
* @Modifier:
* @ModifyTime:
*/
public boolean cacheSet(String k, Set<String> v) {
return cacheSet(k, v, -1);
}
/**
*
* @MethodName:getSet
* @param k
* @return
* @ReturnType:Set<String>
* @Description:获取缓存set数据
* @Creator:chenchuanliang
* @CreateTime:2017年5月18日上午11:17:45
* @Modifier:
* @ModifyTime:
*/
public Set<String> getSet(String k) {
String key = KEY_PREFIX_SET + k;
try {
SetOperations<String, String> setOps = redisTemplate.opsForSet();
return setOps.members(key);
} catch (Throwable t) {
logger.error("获取set缓存失败key[" + key + ", error[" + t + "]");
}
return null;
}
/**
*
* @MethodName:cacheList
* @param k
* @param v
* @param time
* @return
* @ReturnType:boolean
* @Description:list缓存
* @Creator:chenchuanliang
* @CreateTime:2017年5月18日上午11:17:23
* @Modifier:
* @ModifyTime:
*/
public boolean cacheList(String k, String v, long time) {
String key = KEY_PREFIX_LIST + k;
try {
ListOperations<String, String> listOps = redisTemplate.opsForList();
listOps.rightPush(key, v);
if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS);
return true;
} catch (Throwable t) {
logger.error("缓存[" + key + "]失败, value[" + v + "]", t);
}
return false;
}
/**
*
* @MethodName:cacheList
* @param k
* @param v
* @return
* @ReturnType:boolean
* @Description:缓存list
* @Creator:chenchuanliang
* @CreateTime:2017年5月18日上午11:17:10
* @Modifier:
* @ModifyTime:
*/
public boolean cacheList(String k, String v) {
return cacheList(k, v, -1);
}
/**
*
* @MethodName:cacheList
* @param k
* @param v
* @param t
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
jedis.zip (37个子文件)
jedis
pom.xml 5KB
target
test-classes
com
test
jedis
TestConnectRedis.class 2KB
BaseTest.class 279B
Base.class 968B
StartMain.class 1KB
entity
User.class 1KB
m2e-wtp
web-resources
META-INF
maven
com.deshengzheng
jedis
pom.xml 5KB
pom.properties 224B
MANIFEST.MF 113B
classes
config
redis.properties 332B
spring-mvc.xml 2KB
com
deshengzheng
jedis
RedisOperation.class 3KB
controller
RedisService.class 19KB
redis
redis.xml 3KB
.DS_Store 6KB
.settings
org.eclipse.m2e.core.prefs 86B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 245B
org.eclipse.wst.common.component 646B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.validation.prefs 48B
.jsdtscope 626B
org.eclipse.jdt.core.prefs 422B
src
test
java
com
test
jedis
BaseTest.java 52B
StartMain.java 1KB
TestConnectRedis.java 1KB
Base.java 571B
entity
User.java 707B
main
webapp
WEB-INF
web.xml 302B
index.jsp 52B
resources
config
redis.properties 332B
spring-mvc.xml 2KB
redis
redis.xml 3KB
java
com
deshengzheng
jedis
RedisService.java 24KB
RedisOperation.java 1KB
controller
.project 1KB
.classpath 1KB
共 37 条
- 1
资源评论
zhengdesheng19930211
- 粉丝: 8
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功