PageRenderTime 62ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/tts/component/redis/TTSRedisTemplate.java

https://gitlab.com/Fish-Potato/TTStreet-core
Java | 113 lines | 87 code | 19 blank | 7 comment | 10 complexity | db13f5cfe7d6b3c39b09febb4d5f4a68 MD5 | raw file
  1. package com.tts.component.redis;
  2. import com.alibaba.fastjson.JSON;
  3. import org.apache.commons.collections.MapUtils;
  4. import org.springframework.beans.factory.BeanNameAware;
  5. import org.springframework.dao.DataAccessException;
  6. import org.springframework.data.redis.connection.RedisConnection;
  7. import org.springframework.data.redis.core.RedisCallback;
  8. import org.springframework.data.redis.core.RedisTemplate;
  9. import org.springframework.data.redis.core.TimeoutUtils;
  10. import org.springframework.data.redis.serializer.RedisSerializer;
  11. import org.springframework.data.redis.serializer.StringRedisSerializer;
  12. import java.io.UnsupportedEncodingException;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.concurrent.TimeUnit;
  17. import static org.apache.lucene.util.IOUtils.CHARSET_UTF_8;
  18. /**
  19. * Created by zhaoqi on 2016/8/11 0011.
  20. * RedisTemplate封装类
  21. * 根据缓存策略,集群方式不同,操作redis的方式也会不同
  22. */
  23. public class TTSRedisTemplate<K,V> extends RedisTemplate<K,V> implements BeanNameAware {
  24. private RedisTemplate<K,V> redisTemplate;
  25. private String beanName;
  26. private RedisSerializer redisSerializer;
  27. private RedisSerializer defaultSerializer = new StringRedisSerializer();
  28. public RedisTemplate<K, V> getRedisTemplate() {
  29. return redisTemplate;
  30. }
  31. public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
  32. this.redisTemplate = redisTemplate;
  33. }
  34. @SuppressWarnings("unchecked")
  35. public void mset(Map<String, ? extends Object> map, long timeout) {
  36. if (MapUtils.isEmpty(map)) {
  37. return;
  38. }
  39. Map<String, String> cacheMap = new HashMap<String, String>(map.size());
  40. for (String key : map.keySet()) {
  41. Object value = map.get(key);
  42. if (value instanceof String) {
  43. cacheMap.put(key, (String) value);
  44. } else if (value instanceof Integer) {
  45. cacheMap.put(key, value + "");
  46. } else {
  47. cacheMap.put(key, JSON.toJSONString(value));
  48. }
  49. }
  50. try {
  51. executePipelined(new RedisCallback<Object>() {
  52. @Override
  53. public Object doInRedis(RedisConnection connection) throws DataAccessException {
  54. connection.openPipeline();
  55. for (Map.Entry<String, String> item : cacheMap.entrySet()) {
  56. connection.setEx(getRedisSerializer().serialize(item.getKey()), timeout, null == item.getValue() ? null : getRedisSerializer().serialize(item.getValue()));
  57. }
  58. // 无需返回是否缓存成功
  59. return null;
  60. }
  61. });
  62. } catch (Exception e) {
  63. logger.warn("mset into redis failed :{}",e);
  64. }
  65. }
  66. public Boolean longExpire(K key, final long timeout, final TimeUnit unit) {
  67. try {
  68. final byte[] rawKey = String.valueOf(key).getBytes("UTF-8");
  69. return (Boolean) this.execute(new RedisCallback() {
  70. @Override
  71. public Boolean doInRedis(RedisConnection connection) {
  72. return connection.expire(rawKey, TimeoutUtils.toSeconds(timeout, unit));
  73. }
  74. }, true);
  75. } catch (UnsupportedEncodingException e) {
  76. logger.warn("unsupport this encoding :{}",e);
  77. }
  78. return false;
  79. }
  80. public RedisSerializer getRedisSerializer() {
  81. // 没有自定义则返回默认序列化方式
  82. if (null == redisSerializer) {
  83. return defaultSerializer;
  84. }
  85. return redisSerializer;
  86. }
  87. public void setRedisSerializer(RedisSerializer redisSerializer) {
  88. this.redisSerializer = redisSerializer;
  89. }
  90. @Override
  91. public void setBeanName(String name) {
  92. this.beanName=name;
  93. }
  94. }