首页 > 系统服务 > 详细

Icache

时间:2022-05-27 22:30:04      阅读:21      评论:0      收藏:0      [点我收藏+]
package com.cloud.cang.cache.redis;

import java.util.Map;
import java.util.Set;


public interface ICached {

/**
* 设置值,不设置过期时间
* @return
*/
void put(String key,Object value)throws Exception;

/**
* 设置值,不设置过期时间
* @return
*/
void put(String key,Object value, final int expireSec)throws Exception;

/**
* 设置redis超时时间
* @return
*/
void expireSec(String key,final int expireSec)throws Exception;

/**
* 删除 缓存
* @param key
* @return
* @throws Exception
*/
void remove(String key)throws Exception;
/**
* 获取缓存
* @param key
* @return
* @throws Exception
*/
Object get(String key)throws Exception;

/**
* 根据 正则表达式key 获取 列表
* @param key
* @return
* @throws Exception
*/
Set hgetKeys(String key)throws Exception;



/**
* 更新 缓存
* @param key
* @param value
* @return
* @throws Exception
*/
void hset(String key,String hkey,Object value)throws Exception;


/**
* 获取缓存
* @param key
* @return
* @throws Exception
*/
Object hget(String key,String hkey)throws Exception;


/**
* 删除 缓存
* @param key
* @param hkey
* @return
* @throws Exception
*/
Long hremove(String key,String ... hkey)throws Exception;
/**
* 获取 map中的所有值
* @param key
* @return
* @throws Exception
*/
<T> Set<T> hvalues(String key)throws Exception;

/**
* 根据一个key取得对应的map值
* @param key
* @return
* @throws Exception
*/
Map<String,Object> hget(String key) throws Exception;

/**
* 调用一个map对象值
* @param key
* @param vs
* @throws Exception
*/
void hset(String key,Map<String,Object> vs) throws Exception;



/**
* 自增值,并返回增加后的值
* @param key
* @param expireSec 数据有效期
* @return
* @throws Exception
*/
Long incrAndGetForExpire(String key,int expireSec) throws Exception ;

/**
* 自增值,并返回增加后的值(没有设置失效,数据一直存放于redis)
* @param key
* @return
* @throws Exception
*/
Long incrAndGet(String key) throws Exception;

/**
* 自减值,并返回减后的值
* @param key
* @return
* @throws Exception
*/
Long decrAndGet(String key) throws Exception;

/**
* 将元素插入redis列表的左端,并返回列表的大小
* @param key
* @param value
* @return
*/
long lpush(String key,Object value) throws Exception;

/**
* 从redis列表的左端移除元素,并返回列表的大小
* @param key
* @return
*/
Object lpop(String key) throws Exception;

/**
* 判断用户是否存在
* @param key
* @return
* @throws Exception
*/
boolean exists(String key) throws Exception;
/**
* 取得自增序列的变量值
* @param key
* @return
* @throws Exception
*/
Long getIncrOrDecrValue(String key)throws Exception;

/**
* 取得list的大小
* @param key
* @return
* @throws Exception
*/
Long llen(String key) throws Exception;

/**
* 原子增加值
* @param key
* @param val
* @return
* @throws Exception
*/
Long incrByVal(String key, Long val) throws Exception;

/**
* 原子减少值
* @param key
* @param val
* @return
* @throws Exception
*/
Long decrByVal(String key, Long val) throws Exception;

}


//实现


package com.cloud.cang.cache.redis;

import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.cloud.cang.utils.SerializeUtils;
import redis.clients.jedis.JedisCluster;


/**
*
*
* 类功能描述: 实现redis的缓存, 基础redis集群模式
*
* @author zhouhong
* @version V1.0, 2014-5-9
*/
public class RedisCachedImpl implements ICached {


public static final String CHARSET = "UTF-8";
public JedisCluster redisCli;

public void setRedisCli(JedisCluster redisCli) {
this.redisCli = redisCli;
}

@Override
public void put(String key, Object value) throws Exception {
redisCli.set(key.getBytes(CHARSET), SerializeUtils.serialize(value) );
}

@Override
public void put(String key, Object value, int expireSec) throws Exception {
redisCli.setex(key.getBytes(CHARSET), expireSec, SerializeUtils.serialize(value));
}

@Override
public void expireSec(String key, int expireSec) throws Exception {
redisCli.expire(key, expireSec);
}

@Override
public void remove(String key) throws Exception {
redisCli.del(key);
}

@Override
public Object get(String key) throws Exception {
return SerializeUtils.deserialize(redisCli.get(key.getBytes(CHARSET)));
}



@Override
public Set hgetKeys(String key) throws Exception {
return redisCli.hkeys(key);
}

@Override
public void hset(String key, String hkey, Object value) throws Exception {
redisCli.hset(key.getBytes(CHARSET), hkey.getBytes(CHARSET), SerializeUtils.serialize(value) );

}

@Override
public Object hget(String key, String hkey) throws Exception {
return SerializeUtils.deserialize(redisCli.hget(key.getBytes(CHARSET), hkey.getBytes(CHARSET)));
}

@Override
public Long hremove(String key, String ... hkey) throws Exception {
return redisCli.hdel(key, hkey);
}

@SuppressWarnings("unchecked")
@Override
public <T> Set<T> hvalues(String key) throws Exception {
Collection<byte[]> tmp = this.redisCli.hvals(key.getBytes(CHARSET));
Set<T> list = new HashSet<T>();
for (byte[] bs : tmp) {
list.add((T)SerializeUtils.deserialize(bs));
}
return list;
}

@Override
public Long incrAndGetForExpire(String key, int expireSec) throws Exception {
Long value= this.redisCli.incr(key);
this.redisCli.expire(key, expireSec);
return value;
}

public Map<String,Object> hget(String key) throws Exception{
Map<byte[], byte[]> map=this.redisCli.hgetAll(key.getBytes(CHARSET));
Map<String,Object> values=new HashMap<String, Object>();
for(Map.Entry<byte[], byte[]> entry: map.entrySet()) {
values.put(new String(entry.getKey(), CHARSET), SerializeUtils.deserialize(entry.getValue()));
}
return values;
}

public void hset(String key,Map<String,Object> vs) throws Exception{
Map<byte[], byte[]> saveValue=new HashMap<byte[], byte[]>();
for(Map.Entry<String,Object> entry: vs.entrySet()) {
saveValue.put(entry.getKey().getBytes(CHARSET), SerializeUtils.serialize(entry.getValue()));
}
this.redisCli.hmset(key.getBytes(CHARSET), saveValue);
}

@Override
public Long incrAndGet(String key) throws Exception {
return this.redisCli.incr(key);
}

public Long getIncrOrDecrValue(String key)throws Exception {
String val=this.redisCli.get(key);
if(val==null){
return 0l;
}else{
return Long.parseLong(val);
}

}

@Override
public Long decrAndGet(String key) throws Exception {
return this.redisCli.decr(key);
}

@Override
public long lpush(String key, Object value) throws Exception {
return this.redisCli.lpush(key.getBytes(CHARSET), SerializeUtils.serialize(value) );
}

@Override
public Object lpop(String key) throws Exception {
return SerializeUtils.deserialize(this.redisCli.lpop(key.getBytes(CHARSET)));
}

@Override
public boolean exists(String key) throws Exception {
return this.redisCli.exists(key.getBytes(CHARSET));
}
@Override
public Long llen(String key) throws Exception {
return this.redisCli.llen(key);
}
@Override
public Long incrByVal(String key,Long val) throws Exception {
return this.redisCli.incrBy(key, val);
}
@Override
public Long decrByVal(String key,Long val) throws Exception {
return this.redisCli.decrBy(key, val);
}

public static void main(String[] args) {
try {
System.out.println("liqinqin".getBytes(CHARSET));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}

Icache

原文:https://www.cnblogs.com/mimimikasa/p/15347514.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!