package com.hxkj.fsslr.core.utils;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
/** * ClassName:RedisUtils * Function: RedisUtils@author[email protected] Date: July 17, 2016 2:49:45 PM *@version 1.0
* @sinceJDK 1.8 * /
public class RedisUtils {
@SuppressWarnings("rawtypes")
private static RedisTemplate redisTemplate = (RedisTemplate) SpringContextUtils
.getBean("redisTemplate");
/ * * *@Function:
* @Title: save
* @param key
* @param value
* @return
* boolean
* @author [email protected]
* @dateJuly 17, 2016 3:36:36 PM *@SinceJDK 1.8 * /
@SuppressWarnings("unchecked")
public static boolean save(final String key,Object value){
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key,value);
result = true;
} catch (Exception e) {
e.printStackTrace();
return result;
}
return result;
}
/ * * *@Function: Save redis, need to pass in save time *@Title: save
* @param key
* @param value
* @param saveTime
* boolean
* @author [email protected]
* @dateJuly 17, 2016 3:40:21 PM *@SinceJDK 1.8 * /
@SuppressWarnings("unchecked")
public static boolean save(final String key,Object value,Long saveTime){
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key,value);
redisTemplate.expire(key, saveTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
e.printStackTrace();
return result;
}
return result;
}
/ * * *@FunctionValue * via key@Title: get
* @param key
* @return
* Object
* @author [email protected]
* @dateJuly 17, 2016 3:49:21 PM *@SinceJDK 1.8 * /
@SuppressWarnings("unchecked")
public static Object get(final String key){
Object result = null;
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
/ * * *@Function: Checks whether the key-value pair * exists@Title: exists
* @param key
* @return
* boolean
* @author [email protected]
* @dateJuly 17, 2016 3:52:08 PM *@SinceJDK 1.8 * /
@SuppressWarnings("unchecked")
public static boolean exists(final String key){
return redisTemplate.hasKey(key);
}
/ * * *@Function: Deletes a key-value pair *@Title: remove
* @param key
* void
* @author [email protected]
* @dateJuly 17, 2016 3:53:41 PM *@SinceJDK 1.8 * /
@SuppressWarnings("unchecked")
public static void remove(final String key){
if(exists(key)){ redisTemplate.delete(key); }}}Copy the code