静态块初始化redis连接池
static
{
try
{
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);//可用实例连接最大数量,如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)
config.setMaxIdle(MAX_IDLE);//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例
config.setMaxWaitMillis(MAX_WAIT);//等待可用连接的最大时间
config.setTestOnBorrow(TEST_ON_BORROW);//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的
jedisPool = new JedisPool(config, ADDR, PORT);//addr redisIp; port redis端口
}
catch (Exception e)
{
e.printStackTrace();
}
}
获取Jedis实例
private synchronized static Jedis getJedis()
{
try
{
if (jedisPool != null)
{
Jedis resource = jedisPool.getResource();
return resource;
}
else
{
return null;
}
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
redis分布式锁:加锁
/**
* @Description redis分布式锁:加锁
* @param locaName
* 锁的key
* @param acquireTimeout
* 超时时间,超过这个时间则放弃获取锁
* @param timeout
* 锁的超时时间
* @return 锁标识
*/
public static String lockWithTimeout(String locaName, long acquireTimeout, long timeout)
{
Jedis conn = null;
String retIdentifier = null;
try
{
conn = jedisPool.getResource();
String identifier = UUID.randomUUID().toString();// 随机生成一个value
String lockKey = "lock:" + locaName;// 锁名,即key值
int lockExpire = (int) (timeout / 1000);
long end = System.currentTimeMillis() + acquireTimeout;
while (System.currentTimeMillis() < end)
{
if (conn.setnx(lockKey, identifier) == 1)
{
conn.expire(lockKey, lockExpire);
retIdentifier = identifier;// 返回value值,用于释放锁时间确认
return retIdentifier;
}
// 返回-1代表key没有设置超时时间,为key设置一个超时时间
if (conn.ttl(lockKey) == -1)
{
conn.expire(lockKey, lockExpire);
}
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
}
}
catch (JedisException e)
{
e.printStackTrace();
}
finally
{
if (conn != null)
{
conn.close();
}
}
return retIdentifier;
}
redis分布式锁:释放锁
/**
* @Description redis分布式锁:释放锁
* @param lockName
* 锁的key
* @param identifier
* 锁标识(value)
* @return
*/
public static boolean releaseLock(String lockName, String identifier)
{
Jedis conn = null;
String lockKey = "lock:" + lockName;
boolean retFlag = false;
try
{
conn = jedisPool.getResource();
while (true)
{
// 监视lock,准备开始事务
conn.watch(lockKey);
// 通过前面返回的value值判断是不是该锁,若是该锁,则删除,释放锁
if (identifier.equals(conn.get(lockKey)))
{
Transaction transaction = conn.multi();
transaction.del(lockKey);
List<Object> results = transaction.exec();
if (results == null)
{
continue;
}
retFlag = true;
}
conn.unwatch();
break;
}
}
catch (JedisException e)
{
e.printStackTrace();
}
finally
{
if (conn != null)
{
conn.close();
}
}
return retFlag;
}
String:设置一个key、value
public static void set(String key, String value)
{
Jedis jedis = null;
try
{
jedis = getJedis();
jedis.set(key, value);
}
catch (Exception e)
{
// 释放redis对象
e.printStackTrace();
}
finally
{
// 返还到连接池
jedis.close();
}
}
String:设置一个key、有效期和value
public static void setex(String key, int seconds, String value)
{
Jedis jedis = null;
try
{
jedis = getJedis();
jedis.setex(key, seconds, value);
}
catch (Exception e)
{
// 释放redis对象
e.printStackTrace();
}
finally
{
// 返还到连接池
jedis.close();
}
}
String:根据key获取value
public static String get(String key)
{
Jedis jedis = null;
String value = null;
try
{
jedis = getJedis();
value = jedis.get(key);
}
catch (Exception e)
{
// 释放redis对象
e.printStackTrace();
}
finally
{
// 返还到连接池
jedis.close();
}
return value;
}
String:查询key是否存在
public static boolean isExists(String key)
{
boolean b = false;
Jedis jedis = null;
try
{
jedis = getJedis();
b = jedis.exists(key);
}
catch (Exception e)
{
// 释放redis对象
e.printStackTrace();
}
finally
{
// 返还到连接池
jedis.close();
}
return b;
}
Hash:保存key、hset对象及对应的值
public static void hset(String key, String objName, String value)
{
Jedis jedis = null;
try
{
jedis = getJedis();
jedis.hset(key, objName, value);
}
catch (Exception e)
{
// 释放redis对象
e.printStackTrace();
}
finally
{
// 返还到连接池
jedis.close();
}
}
Hash:根据key、对象名获取数据
public static String hget(String key, String objName)
{
String value = null;
Jedis jedis = null;
try
{
jedis = getJedis();
value = jedis.hget(key, objName);
}
catch (Exception e)
{
// 释放redis对象
e.printStackTrace();
}
finally
{
// 返还到连接池
jedis.close();
}
return value;
}
Hash:查看key、field是否存在
public static boolean hexists(String key, String field)
{
boolean b = false;
Jedis jedis = null;
try
{
jedis = getJedis();
b = jedis.hexists(key, field);
}
catch (Exception e)
{
// 释放redis对象
e.printStackTrace();
}
finally
{
// 返还到连接池
jedis.close();
}
return b;
}
Set:set集合
public static void sadd(String key, String member)
{
Jedis jedis = null;
try
{
jedis = getJedis();
jedis.sadd(key, member);
}
catch (Exception e)
{
// 释放redis对象
e.printStackTrace();
}
finally
{
// 返还到连接池
jedis.close();
}
}
Set:随机返回并移除一个元素
public static String spop(String key)
{
String value = null;
Jedis jedis = null;
try
{
jedis = getJedis();
value = jedis.spop(key);
}
catch (Exception e)
{
// 释放redis对象
e.printStackTrace();
}
finally
{
// 返还到连接池
jedis.close();
}
return value;
}
返回redis长度
public static Long scard(String key)
{
Long value = null;
Jedis jedis = null;
try
{
jedis = getJedis();
value = jedis.scard(key);
}
catch (Exception e)
{
// 释放redis对象
e.printStackTrace();
}
finally
{
// 返还到连接池
jedis.close();
}
return value;
}
删除redis
public static void del(String key)
{
Jedis jedis = null;
try
{
jedis = getJedis();
jedis.del(key);
}
catch (Exception e)
{
// 释放redis对象
e.printStackTrace();
}
finally
{
// 返还到连接池
jedis.close();
}
}