package com.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.stereotype.Component;
@Component
public class RedisClient {
@Autowired
private JedisConnectionFactory factory;
/**
* GET key 杩斿洖key鎵�叧鑱旂殑瀛楃涓插�
*
* @param key
* @return
*/
public String get(String key) {
String value = null;
JedisConnection connection = null;
try
{
connection = factory.getConnection();
byte[] valueByte = connection.get(key.getBytes());
if (valueByte != null)
{
value = new String(valueByte);
}
}
catch (Exception e)
{
}
finally
{
if(connection != null){
connection.close();
}
}
return value;
}
/**
* SET key value灏嗗瓧绗︿覆鍊紇alue鍏宠仈鍒発ey(瑕嗙洊鏃у�)銆�
*
* @param key
* @param value
*/
public void set(String key, String value) {
JedisConnection connection = null;
try
{
connection = factory.getConnection();
connection.set(key.getBytes(), value.getBytes());
}
catch (Exception e)
{
}
finally
{
if(connection != null){
connection.close();
}
}
}
/**
* APPEND key value濡傛灉key宸茬粡瀛樺湪骞朵笖鏄竴涓瓧绗︿覆锛孉PPEND鍛戒护灏唙alue杩藉姞鍒発ey鍘熸潵鐨勫�涔嬪悗銆�
*
* @param key
* @param value
*/
public void append(String key, String value) {
JedisConnection connection = null;
try
{
connection = factory.getConnection();
connection.append(key.getBytes(), value.getBytes());
}
catch (Exception e)
{
}
finally
{
if(connection != null){
connection.close();
}
}
}
/**
* RENAME key newkey
* 灏唊ey鏀瑰悕涓簄ewkey銆傚綋key鍜宯ewkey鐩稿悓鎴栬�key涓嶅瓨鍦ㄦ椂锛岃繑鍥炰竴涓敊璇�褰搉ewkey宸茬粡瀛樺湪鏃讹紝RENAME鍛戒护灏嗚鐩栨棫鍊笺�
*
* @param key
* @param newKey
*/
public void rename(String key, String newKey) {
JedisConnection connection = null;
try
{
connection = factory.getConnection();
connection.rename(key.getBytes(), newKey.getBytes());
}
catch (Exception e)
{
}
finally
{
if(connection != null){
connection.close();
}
}
}
/**
* TYPE key 杩斿洖key鎵�偍瀛樼殑鍊肩殑绫诲瀷銆�
*
* @param key
* @return none(key涓嶅瓨鍦�,string(瀛楃涓�,list(鍒楄〃),set(闆嗗悎),zset(鏈夊簭闆�,hash(鍝堝笇琛�
*/
public String type(String key) {
DataType type = null;
JedisConnection connection = null;
try
{
connection = factory.getConnection();
type = connection.type(key.getBytes());
}
catch (Exception e)
{
}
finally
{
if(connection != null){
connection.close();
}
}
return type.toString();
}
/**
* SETEX key seconds value灏嗗�value鍏宠仈鍒発ey锛屽苟灏唊ey鐨勭敓瀛樻椂闂磋涓簊econds(浠ョ涓哄崟浣�銆�
*
* @param key
* @param timeOut
* @param value
*/
public void setEx(String key, long timeOut, String value) {
JedisConnection connection = null;
try
{
connection = factory.getConnection();
connection.setEx(key.getBytes(), timeOut, value.getBytes());
}
catch (Exception e)
{
}
finally
{
if(connection != null){
connection.close();
}
}
}
/**
* EXPIRE key seconds 涓虹粰瀹歬ey璁剧疆鐢熷瓨鏃堕棿銆傚綋key杩囨湡鏃讹紝瀹冧細琚嚜鍔ㄥ垹闄ゃ�
*
* @param key
* @param timeOut
* (鍗曚綅:绉�
* @return
*/
public boolean expire(String key, long timeOut) {
boolean f = false;
JedisConnection connection = null;
try
{
connection = factory.getConnection();
f = connection.expire(key.getBytes(), timeOut);
}
catch (Exception e)
{
}
finally
{
if(connection != null){
connection.close();
}
}
return f;
}
/**
* EXPIREAT
* EXPIREAT鐨勪綔鐢ㄥ拰EXPIRE涓�牱锛岄兘鐢ㄤ簬涓簁ey璁剧疆鐢熷瓨鏃堕棿銆備笉鍚屽湪浜嶦XPIREAT鍛戒护鎺ュ彈鐨勬椂闂村弬鏁版槸UNIX鏃堕棿鎴�unix
* timestamp)
*
* @param key
* @param timestamp
* (UNIX鏃堕棿鎴�unix timestamp) )
* @return
*/
public boolean expireAt(String key, long timestamp) {
boolean f = false;
JedisConnection connection = null;
try
{
connection = factory.getConnection();
f = connection.expireAt(key.getBytes(), timestamp);
}
catch (Exception e)
{
}
finally
{
if(connection != null){
connection.close();
}
}
return f;
}
/**
* TTL 杩斿洖缁欏畾key鐨勫墿浣欑敓瀛樻椂闂�time to live)(浠ョ涓哄崟浣�
*
* @param key
* @return
*/
public int ttl(String key) {
Long time = 0L;
JedisConnection connection = null;
try
{
connection = factory.getConnection();
time = connection.ttl(key.getBytes());
}
catch (Exception e)
{
}
finally
{
if(connection != null){
connection.close();
}
}
return time.intValue();
}
/**
* PERSIST key 绉婚櫎缁欏畾key鐨勭敓瀛樻椂闂淬�
*
* @param key
* @return
*/
public boolean persist(String key) {
boolean f = false;
JedisConnection connection = null;
try
{
connection = factory.getConnection();
f = connection.persist(key.getBytes());
}
catch (Exception e)
{
}
finally
{
if(connection != null){
connection.close();
}
}
return f;
}
/**
* EXISTS 妫�煡缁欏畾key鏄惁瀛樺湪銆�
*
* @param key
* @return
*/
public boolean exists(String key) {
boolean f = false;
JedisConnection connection = null;
try
{
connection = factory.getConnection();
f = connection.exists(key.getBytes());
}
catch (Exception e)
{
}
finally
{
if(connection != n
没有合适的资源?快使用搜索试试~ 我知道了~
spring集成redis,配置加代码例子
共34个文件
xml:8个
properties:7个
prefs:4个
需积分: 34 22 下载量 163 浏览量
2017-09-17
15:42:50
上传
评论
收藏 21KB RAR 举报
温馨提示
springmvc框架加redis集成,redis直接在配置文件配置即可使用,实现了string,list,hashmap以及对象存储,将对象序列化成二进制格式存储,取出将二进制反序列化,代码有描述及例子
资源推荐
资源详情
资源评论
收起资源包目录
springmvc.rar (34个子文件)
springmvc
pom.xml 7KB
target
test-classes
m2e-wtp
web-resources
META-INF
maven
springmvc
springmvc
pom.xml 7KB
pom.properties 214B
MANIFEST.MF 114B
classes
redis.properties 179B
spring-mvc.xml 3KB
jdbc.properties 153B
log4j.properties 746B
com
action
Bmaction.class 1KB
redis
StartAddCacheListener.class 1KB
RedisClient.class 5KB
applicationContext.xml 3KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.core.resources.prefs 89B
org.eclipse.wst.common.component 752B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.validation.prefs 50B
.jsdtscope 639B
org.eclipse.jdt.core.prefs 430B
src
test
java
main
webapp
weixin.jsp 989B
WEB-INF
web.xml 3KB
index.jsp 436B
resources
redis.properties 179B
spring-mvc.xml 3KB
jdbc.properties 153B
log4j.properties 746B
applicationContext.xml 3KB
java
com
action
Bmaction.java 777B
redis
RedisClient.java 8KB
StartAddCacheListener.java 378B
.project 1KB
.classpath 1KB
共 34 条
- 1
资源评论
龚-gxq
- 粉丝: 1
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功