Map acts as a buffer

  . 1  public  class the MapCache {
   2  
  . 3      / ** 
  . 4       * 1024 default cache memory
   . 5       * / 
  . 6      Private  static  Final  int DEFAULT_CACHES = 1024 ;
   . 7  
  . 8      Private  static  Final the MapCache the INS = new new the MapCache ();
   . 9  
10      public  static the MapCache SINGLE () {
 . 11          return the INS;
 12 is      }
 13 is  
14      / ** 
15       * buffer vessel
 16       * / 
. 17      Private static the Map <String, CacheObject> cachePool;
 18 is  
. 19      public the MapCache () {
 20 is          the this (DEFAULT_CACHES);
 21 is      }
 22 is  
23 is      public the MapCache ( int cacheCount) {
 24          cachePool = new new of ConcurrentHashMap (cacheCount);
 25      }
 26 is  
27      / ** 
28       * a read buffer
 29       *
 30       * @param Key cache Key
 31 is       * @param <T>
 32       * @return
 33      */
 34     public static <T> T get(String key) {
 35         CacheObject cacheObject = cachePool.get(key);
 36         if (null != cacheObject) {
 37             long cur = System.currentTimeMillis() / 1000;
 38             if (cacheObject.getExpired() <= 0 || cacheObject.getExpired() > cur) {
 39                 Object result = cacheObject.getValue();
 40                 return (T) result;
 41             }
 42         }
 43         return null ;
 44 is      }
 45  
46 is      / ** 
47       * read a cache hash type
 48       *
 49       * @param Key cache Key
 50       * @param Field cache Field
 51 is       * @param <T>
 52 is       * @return 
53 is       * / 
54 is      public < T> T hget (String Key, String Field) {
 55          Key = Key + ":" + Field;
 56 is          return  the this .get (Key);
 57 is      }
 58  
59      / **
60       * is provided a buffer
 61 is       *
 62 is       * @param Key cache Key
 63 is       * @param value buffer value
 64       * / 
65      public  static  void SET (String Key, Object value) {
 66          INS.set (Key, value, -1 );
 67      }
 68  
69      / * 
70       * set the expiration time with a cache and
 71 is       *
 72       * @param Key cache Key
 73 is       * @param value buffer value
 74       * @paramexpired expiration time, seconds
 75       * / 
76      public  void SET (String Key, Object value, Long expired) {
 77          expired expired => 0 System.currentTimeMillis () / 1000 +? expired: expired;
 78          CacheObject cacheObject = new new CacheObject (Key, value, expired The);
 79          cachePool.put (Key, cacheObject);
 80      }
 81  
82      / ** 
83       * hash provided a buffer
 84       *
 85       * @param Key cache Key
 86       * @param field buffers field
87       * @param value buffer value
 88       * / 
89      public  void HSET (String Key, Field String, Object value) {
 90          the this .hset (Key, Field, value, -1 );
 91 is      }
 92  
93      / ** 
94       * Set and a hash cache expiration time with
 95       *
 96       * @param Key cache Key
 97       * @param Field cache Field
 98       * @param value buffer value
 99       * @param expired The expiration time, seconds
 100       * /
101     public void hset(String key, String field, Object value, long expired) {
102         key = key + ":" + field;
103         expired = expired > 0 ? System.currentTimeMillis() / 1000 + expired : expired;
104         CacheObject cacheObject = new CacheObject(key, value, expired);
105         cachePool.put(key, cacheObject);
106     }
107 
108     /**
109      * 根据key删除缓存
110      *
111      * @param key 缓存key
112      */
113     public  static  void del (String key) {
 114          cachePool.remove (key);
 115      }
 1 16  
117      / ** 
1 18       * The key field and delete the cache
 119       *
 120       * @param key cache key
 121       * @param field buffer field
 122       * / 
123      public  void HDEL (String Key, String Field) {
 124          Key + Key = ":" + Field;
 125          the this .del (Key);
 126      }
 127  
128      / ** 
129      * 清空缓存
130      */
131     public void clean() {
132         cachePool.clear();
133     }
134 
135     static class CacheObject {
136         private String key;
137         private Object value;
138         private long expired;
139 
140         public CacheObject(String key, Object value, long expired) {
141             this.key = key;
142             this.value = value;
143             this.expired = expired;
144         }
145 
146         public String getKey() {
147             return key;
148         }
149 
150         public Object getValue() {
151             return value;
152         }
153 
154         public long getExpired() {
155             return expired;
156         }
157     }
158 }

Reprinted at: https: //blog.csdn.net/xsj_blog/article/details/83056143

Guess you like

Origin www.cnblogs.com/wsycoo/p/12058032.html