import java.util.Map;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* 缓存操作类
* @author
*
*/
public class JedisUtil {
/**
* 新增APP用户至缓存
* @param token
* @param obj
*/
public static void addAppUser(String token,Map<String,String> map){
//获取JedisPool
JedisPool jedisPool =PoolUtil.getJedisPoolInstance();
//获取Jedis
Jedis jedis = jedisPool.getResource();
//存储Jedis
jedis.hmset(token, map);
//释放Jedis
PoolUtil.release(jedisPool, jedis);
}
/**
* 根据token获取App用户缓存
* @param token
* @return
*/
public static Map<String,String> getAppUser(String token){
//获取JedisPool
JedisPool jedisPool =PoolUtil.getJedisPoolInstance();
//获取Jedis
Jedis jedis = jedisPool.getResource();
Map<String,String> map = jedis.hgetAll(token);
//释放Jedis
PoolUtil.release(jedisPool, jedis);
return map;
}
/**
* 新增后台用户至缓存
* @param userId
* @param obj
*/
public static void addSystemUser(String userId,Map<String, String> map){
//获取JedisPool
JedisPool jedisPool =PoolUtil.getJedisPoolInstance();
//获取Jedis
Jedis jedis = jedisPool.getResource();
//存储Jedis
jedis.hmset(userId, map);
//设置有效时间
jedis.expire(userId, 60*60*24);
PoolUtil.release(jedisPool, jedis);
}
/**
* 根据userId获取系统用户缓存
* @param userId
* @return
*/
public static Map<String,String> getSystemUser(String userId){
//获取JedisPool
JedisPool jedisPool =PoolUtil.getJedisPoolInstance();
//获取Jedis
Jedis jedis = jedisPool.getResource();
Map<String,String> map = jedis.hgetAll(userId);
PoolUtil.release(jedisPool, jedis);
return map;
}
/**
* 缓存系统常量
* @param obj
*/
public static void addSystemConstant(Map<String,String> map){
//获取JedisPool
JedisPool jedisPool =PoolUtil.getJedisPoolInstance();
//获取Jedis
Jedis jedis = jedisPool.getResource();
//存储Jedis
for(String key:map.keySet()){
jedis.set(key, map.get(key));
}
PoolUtil.release(jedisPool, jedis);
}
/**
* 获取系统常量缓存
* @param key
* @return
*/
public static String getSystemConstant(String key){
//获取JedisPool
JedisPool jedisPool =PoolUtil.getJedisPoolInstance();
//获取Jedis
Jedis jedis = jedisPool.getResource();
String value = jedis.get(key);
PoolUtil.release(jedisPool, jedis);
return value;
}
/**
* 更新系统常量
* @param key
* @param value
*/
public static void updateSystemConstant(String key,String value)
{
JedisPool jedisPool =PoolUtil.getJedisPoolInstance();
//获取Jedis
Jedis jedis = jedisPool.getResource();
PoolUtil.removeKey(key);
jedis.set(key, value);
PoolUtil.release(jedisPool, jedis);
}
}
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
package com.guduo.common.utils.jedis;
import java.util.Map;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* Jedis连接池
* @author liuguangping
*
*/
public class PoolUtil {
private PoolUtil() {}
private static JedisPool readJedisPool = null;
/**
* 获取JedisPool的实例化--读
* @return
*/
public static JedisPool getJedisPoolInstance()
{
if(null == readJedisPool){
synchronized (PoolUtil.class) {
if(null == readJedisPool){
JedisPoolConfig config = new PoolUtil().setJedisPoolConfig();
readJedisPool = new JedisPool(config,"192.168.1.52",6379);
}
}
}
return readJedisPool;
}
private JedisPoolConfig setJedisPoolConfig(){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(1000);
config.setMaxWaitMillis(100);//请求最短时间
config.setMaxIdle(32);//
config.setTestOnBorrow(true); //设置检测连通性
return config;
}
/**
* 释放连接池
* @param pool
* @param jedis
*/
@SuppressWarnings("deprecation")
public static void release(JedisPool pool,Jedis jedis){
if(null != jedis){
pool.returnResourceObject(jedis);
}
}
/**
* 获取jedis实例
* @return
*/
public static Jedis getJedis(){
Jedis jedis = getJedisPoolInstance().getResource();
return jedis;
}
/**
* 存储Hash:指定存活时间
* @param key
* @param map
* @param times:存活时间(s)
*/
public static void insertHash(String key,Map<String, String> map,int times){
JedisPool pool = getJedisPoolInstance();
Jedis jedis = pool.getResource();
try {
jedis.hmset(key, map);
jedis.expire(key, times);
} catch (Exception e) {
e.printStackTrace();
}finally {
release(pool, jedis);
}
}
/**
* 存储Hash:永久存在
* @param key
* @param map
*/
public static void insertHash(String key,Map<String, String> map){
JedisPool pool = getJedisPoolInstance();
Jedis jedis = pool.getResource();
try {
jedis.hmset(key, map);
} catch (Exception e) {
e.printStackTrace();
}finally {
release(pool, jedis);
}
}
/**
* 存储String:指定存活时间
* @param key
* @param value
* @param times 存活时间(s)
*/
public static void insertString(String key,String value,int times){
JedisPool pool = getJedisPoolInstance();
Jedis jedis = pool.getResource();
try {
jedis.setex(key, times, value);
} catch (Exception e) {
e.printStackTrace();
}finally {
release(pool, jedis);
}
}
/**
* 存储String:默认永久存在
* @param key
* @param value
*/
public static void insertString(String key,String value){
JedisPool pool = getJedisPoolInstance();
Jedis jedis = pool.getResource();
try {
// jedis.setex(key, value);
jedis.set(key, value);
} catch (Exception e) {
e.printStackTrace();
}finally {
release(pool, jedis);
}
}
/**
* 批量存储String
* @param key
* @param map
*/
public static void batchInsertString(Map<String,Object> map){
JedisPool pool = getJedisPoolInstance();
Jedis jedis = pool.getResource();
try {
for(String key:map.keySet()){
if(map.get(key).toString().equals(""))
{
jedis.set(key,"");
}else{
jedis.set(key, map.get(key).toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
release(pool, jedis);
}
}
/**
* 获取Hash
* @param key
* @return
*/
public static Map<String,String> getHash(String key){
Map<String,String> map = null;
JedisPool pool = getJedisPoolInstance();
Jedis jedis = pool.getResource();
try {
map = jedis.hgetAll(key);
} catch (Exception e) {
e.printStackTrace();
}finally {
release(pool, jedis);
}
return map;
}
/**
* 获取单个String
* @param key
* @return
*/
public static String getString(String key){
String value = null;
JedisPool pool = getJedisPoolInstance();
Jedis jedis = pool.getResource();
try {
value = jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
}finally {
release(pool, jedis);
}
return value;
}
/**
* 判断key是否存在
* @param key
* @return
*/
public static boolean checkIsExit(String key){
boolean isExit = false;
JedisPool pool = getJedisPoolInstance();
Jedis jedis = pool.getResource();
try {
isExit = jedis.exists(key);
} catch (Exception e) {
e.printStackTrace();
}finally {
release(pool, jedis);
}
return isExit;
}
/**
* 删除key
* @param key
*/
public static void removeKey(String key){
JedisPool pool = getJedisPoolInstance();
Jedis jedis = pool.getResource();
try {
jedis.del(key);
} catch (Exception e) {
e.printStackTrace();
} finally{
release(pool, jedis);
}
}
}