MappingCache.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.java110.utils.cache;
  2. import com.java110.entity.mapping.Mapping;
  3. import com.java110.utils.constant.DomainContant;
  4. import com.java110.utils.util.SerializeUtil;
  5. import redis.clients.jedis.Jedis;
  6. import java.util.List;
  7. /**
  8. * 映射缓存工具类
  9. * Created by wuxw on 2018/4/14.
  10. */
  11. public class MappingCache extends BaseCache {
  12. //后缀 用来刷缓存时删除 所有以这个为后缀的数据
  13. public final static String _SUFFIX_MAPPING = "_SUFFIX_MAPPING";
  14. /**
  15. * 获取值
  16. *
  17. * @param domain
  18. * @param key
  19. * @return
  20. */
  21. public static String getValue(String domain, String key) {
  22. Jedis redis = null;
  23. try {
  24. redis = getJedis();
  25. Object object = SerializeUtil.unserialize(redis.get((domain + key + _SUFFIX_MAPPING).getBytes()));
  26. if (object == null) {
  27. return null;
  28. }
  29. Mapping mapping = (Mapping) object;
  30. return mapping.getValue();
  31. } finally {
  32. if (redis != null) {
  33. redis.close();
  34. }
  35. }
  36. }
  37. /**
  38. * 获取公用域下的key值
  39. *
  40. * @param key
  41. * @return
  42. */
  43. public static String getValue(String key) {
  44. Mapping mapping = getMapping(key);
  45. return mapping == null ? "" : mapping.getValue();
  46. }
  47. public static Mapping getMapping(String key) {
  48. Jedis redis = null;
  49. try {
  50. redis = getJedis();
  51. Object obj = SerializeUtil.unserialize(redis.get((DomainContant.COMMON_DOMAIN + key + _SUFFIX_MAPPING).getBytes()));
  52. if (obj instanceof Mapping) {
  53. return (Mapping) obj;
  54. }
  55. } finally {
  56. if (redis != null) {
  57. redis.close();
  58. }
  59. }
  60. return null;
  61. }
  62. /**
  63. * 获取 域下的所有数据
  64. *
  65. * @param domain
  66. * @return
  67. */
  68. public static List<Mapping> getValueByDomain(String domain) {
  69. Jedis redis = null;
  70. try {
  71. redis = getJedis();
  72. return SerializeUtil.unserializeList(redis.get((domain + _SUFFIX_MAPPING).getBytes()), Mapping.class);
  73. } finally {
  74. if (redis != null) {
  75. redis.close();
  76. }
  77. }
  78. }
  79. /**
  80. * 保存数据
  81. *
  82. * @param mapping
  83. */
  84. public static void setVaule(Mapping mapping) {
  85. Jedis redis = null;
  86. try {
  87. redis = getJedis();
  88. redis.set((mapping.getDomain() + mapping.getKey() + _SUFFIX_MAPPING).getBytes(), SerializeUtil.serialize(mapping));
  89. } finally {
  90. if (redis != null) {
  91. redis.close();
  92. }
  93. }
  94. }
  95. /**
  96. * 保存list 数据
  97. *
  98. * @param mappings
  99. */
  100. public static void setValue(List<Mapping> mappings) {
  101. Jedis redis = null;
  102. try {
  103. redis = getJedis();
  104. redis.set((mappings.get(0).getDomain() + _SUFFIX_MAPPING).getBytes(), SerializeUtil.serializeList(mappings));
  105. } finally {
  106. if (redis != null) {
  107. redis.close();
  108. }
  109. }
  110. }
  111. /**
  112. * 获取值
  113. *
  114. * @param domain
  115. * @param key
  116. * @return
  117. */
  118. public static String getRemark(String domain, String key) {
  119. Jedis redis = null;
  120. try {
  121. redis = getJedis();
  122. Object object = SerializeUtil.unserialize(redis.get((domain + key + _SUFFIX_MAPPING).getBytes()));
  123. if (object == null) {
  124. return null;
  125. }
  126. Mapping mapping = (Mapping) object;
  127. return mapping.getRemark();
  128. } finally {
  129. if (redis != null) {
  130. redis.close();
  131. }
  132. }
  133. }
  134. }