Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库。
特点:
优势:
使用底层API: RedisConnection操作Redis,需要对数据进行手动转换(String <---->byte),需要进行多数重复性工作,效率低下;org.springframework.data.redis.core.RedisTemplate
类提供了与Redis交互的高级抽象,该类负责序列化和连接管理,对大多数重复性工作进行了封装。并且 RedisTemplate是一个线程安全的类
接口 | 描述 |
---|---|
Key类型操作 | |
ValueOperations | 操作Redis String(或者Value)类型数据 |
ListOperations | 操作Redis List类型数据 |
SetOperations | 操作Redis Set类型数据 |
ZSetOperations | 操作Redis ZSet(或者Sorted Set)类型数据 |
HashOperations | 操作Redis Hash类型数据 |
HyperLogLogOperations | 操作Redis HyperLogLog类型数据,比如:pfadd,pfcount,… |
GeoOperations | 操作Redis Geospatial类型数据,比如:GEOADD,``GEORADIUS ,…) |
Key绑定操作 | |
BoundValueOperations | Redis字符串(或值)键绑定操作 |
BoundListOperations | Redis列表键绑定操作 |
BoundSetOperations | Redis Set键绑定操作 |
BoundZSetOperations | Redis ZSet(或Sorted Set)键绑定操作 |
BoundHashOperations | Redis Hash键绑定操作 |
BoundGeoOperations | Redis Geospatial 键绑定操作 |
@Configuration
public class RedisConfig {
@Autowired
private RedisConnectionFactory factory;
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
}
@Bean
public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
return redisTemplate.opsForValue();
}
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
}
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
}
@Component
@Slf4j
public class RedisUtils {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private ValueOperations<String, String> valueOperations;
@Autowired
private HashOperations<String, String, Object> hashOperations;
@Autowired
private ListOperations<String, Object> listOperations;
@Autowired
private SetOperations<String, Object> setOperations;
@Autowired
private ZSetOperations<String, Object> zSetOperations;
/**
* 默认过期时长,单位:秒
*/
public final static long DEFAULT_EXPIRE = 60 * 60 * 12;
/**
* 不设置过期时长
*/
public final static long NOT_EXPIRE = -1;
private final static Gson GSON = new Gson();
public void set(String key, Object value, long expire) {
try {
valueOperations.set(key, toJson(value));
if (expire != NOT_EXPIRE) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
} catch (Exception ex) {
log.error(ex.getMessage());
}
}
public void set(String key, Object value) {
set(key, value, DEFAULT_EXPIRE);
}
public <T> T get(String key, Class<T> clazz, long expire) {
try {
String value = valueOperations.get(key);
if (expire != NOT_EXPIRE) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return value == null ? null : fromJson(value, clazz);
} catch (Exception ex) {
log.error(ex.getMessage());
}
return null;
}
public <T> T get(String key, Class<T> clazz) {
return get(key, clazz, NOT_EXPIRE);
}
public String get(String key, long expire) {
try {
String value = valueOperations.get(key);
if (expire != NOT_EXPIRE) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return value;
} catch (Exception ex) {
log.error(ex.getMessage());
}
return null;
}
public String get(String key) {
return get(key, NOT_EXPIRE);
}
public void delete(String key) {
try {
redisTemplate.delete(key);
} catch (Exception ex) {
log.error(ex.getMessage());
}
}
/**
* Object转成JSON数据
*/
private String toJson(Object object) {
if (object instanceof Integer || object instanceof Long || object instanceof Float ||
object instanceof Double || object instanceof Boolean || object instanceof String) {
return String.valueOf(object);
}
return GSON.toJson(object);
}
/**
* JSON数据,转成Object
*/
private <T> T fromJson(String json, Class<T> clazz) {
return GSON.fromJson(json, clazz);
}
}