|
| 1 | +package com.dashuai.learning.memcached.manager; |
| 2 | + |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | +import net.spy.memcached.MemcachedClient; |
| 5 | +import net.spy.memcached.internal.OperationFuture; |
| 6 | +import org.springframework.beans.factory.annotation.Value; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.net.InetSocketAddress; |
| 10 | +import java.util.Collection; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.concurrent.Future; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | + |
| 15 | +/** |
| 16 | + * Opeartion memcached manager |
| 17 | + * <p/> |
| 18 | + * Created in 2018.11.15 |
| 19 | + * <p/> |
| 20 | + * |
| 21 | + * @author Liaozihong |
| 22 | + */ |
| 23 | +@Slf4j |
| 24 | +public class OpeartionMemcachedManager { |
| 25 | + /** |
| 26 | + * The Memcached ip. |
| 27 | + */ |
| 28 | + @Value("${memcached.ip}") |
| 29 | + String memcachedIp; |
| 30 | + /** |
| 31 | + * The Memcached port. |
| 32 | + */ |
| 33 | + @Value("${memcached.port}") |
| 34 | + Integer memcachedPort; |
| 35 | + |
| 36 | + /** |
| 37 | + * The constant DEFAULT_TIMEOUT. |
| 38 | + */ |
| 39 | + public final static int DEFAULT_TIMEOUT = 5; |
| 40 | + /** |
| 41 | + * The constant timeUnitSeconds. |
| 42 | + */ |
| 43 | + public final static TimeUnit timeUnitSeconds = TimeUnit.SECONDS; |
| 44 | + |
| 45 | + private MemcachedClient memcachedClient; |
| 46 | + |
| 47 | + /** |
| 48 | + * 初始化 |
| 49 | + */ |
| 50 | + public void init() { |
| 51 | + try { |
| 52 | + //只采用单机模式,如果需要配置集群模式可用AddrUtil.getAddresses(servers), |
| 53 | + //可参考:https://blog.csdn.net/gtuu0123/article/details/4849905 |
| 54 | + memcachedClient = new MemcachedClient(new InetSocketAddress(memcachedIp, memcachedPort)); |
| 55 | + log.info("++++++++++++++++++++ Memcached 连接成功,Address:{}:{} ++++++++++++++++++++++", memcachedIp, memcachedPort); |
| 56 | + } catch (IOException e) { |
| 57 | + log.info("++++++++++++++++++++ Memcached 连接异常,Address:{}:{} ++++++++++++++++++++++{}", memcachedIp, memcachedPort, e); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * 设置键值 |
| 63 | + * |
| 64 | + * @param key 键 |
| 65 | + * @param expire 有效时间 |
| 66 | + * @param value 值 |
| 67 | + * @return boolean boolean |
| 68 | + */ |
| 69 | + public Boolean set(String key, int expire, Object value) { |
| 70 | + OperationFuture<Boolean> result = memcachedClient.set(key, expire, value); |
| 71 | + return getResult(result); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * 根据键获取值 |
| 76 | + * |
| 77 | + * @param key the key |
| 78 | + * @return the object |
| 79 | + */ |
| 80 | + public Object get(String key) { |
| 81 | + return memcachedClient.get(key); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * 以异步的方式获取值 |
| 86 | + * |
| 87 | + * @param key the key |
| 88 | + * @return the object |
| 89 | + */ |
| 90 | + public Object ascynGet(String key) { |
| 91 | + Future<Object> objectFuture = memcachedClient.asyncGet(key); |
| 92 | + return getResult(objectFuture); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * 将对象添加到缓存 |
| 97 | + * |
| 98 | + * @param key the key |
| 99 | + * @param value the value |
| 100 | + * @param expire the expire |
| 101 | + * @return the boolean |
| 102 | + */ |
| 103 | + public Boolean add(String key, Object value, int expire) { |
| 104 | + Future<Boolean> f = memcachedClient.add(key, expire, value); |
| 105 | + return getResult(f); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * 替换某个键值 |
| 110 | + * |
| 111 | + * @param key the 键 |
| 112 | + * @param value the 值 |
| 113 | + * @param expire the 过期时间 |
| 114 | + * @return the boolean |
| 115 | + */ |
| 116 | + public Boolean replace(String key, Object value, int expire) { |
| 117 | + Future<Boolean> f = memcachedClient.replace(key, expire, value); |
| 118 | + return getResult(f); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * 删除某个特定键 |
| 123 | + * |
| 124 | + * @param key the key |
| 125 | + * @return the boolean |
| 126 | + */ |
| 127 | + public Boolean delete(String key) { |
| 128 | + Future<Boolean> f = memcachedClient.delete(key); |
| 129 | + return getResult(f); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * 立即从所有服务器清除所有缓存,慎用。 |
| 134 | + * |
| 135 | + * @return the boolean |
| 136 | + */ |
| 137 | + @Deprecated |
| 138 | + public Boolean flush() { |
| 139 | + Future<Boolean> f = memcachedClient.flush(); |
| 140 | + return getResult(f); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * 从缓存中获取多个键值。 |
| 145 | + * |
| 146 | + * @param keys the 键集合 |
| 147 | + * @return the multi |
| 148 | + */ |
| 149 | + public Map<String, Object> getMulti(Collection<String> keys) { |
| 150 | + return memcachedClient.getBulk(keys); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * 从缓存中获取多个键值 |
| 155 | + * |
| 156 | + * @param keys the 键数组 |
| 157 | + * @return the multi |
| 158 | + */ |
| 159 | + public Map<String, Object> getMulti(String[] keys) { |
| 160 | + return memcachedClient.getBulk(keys); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * 异步地从缓存中获取一组对象并使用它们进行解码 |
| 165 | + * |
| 166 | + * @param keys the 键集合 |
| 167 | + * @return the map |
| 168 | + */ |
| 169 | + public Map<String, Object> asyncGetMulti(Collection<String> keys) { |
| 170 | + Map<String, Object> map = null; |
| 171 | + Future<Map<String, Object>> f = memcachedClient.asyncGetBulk(keys); |
| 172 | + try { |
| 173 | + map = getResult(f); |
| 174 | + } catch (Exception e) { |
| 175 | + f.cancel(false); |
| 176 | + } |
| 177 | + return map; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * 增加给定的计数器,返回新值。 |
| 182 | + * |
| 183 | + * @param key the key |
| 184 | + * @param by the 增值 |
| 185 | + * @param defaultValue the 默认值(如计时器不存在),如该key没值,则取默认值 |
| 186 | + * @param expire the 过期时间 |
| 187 | + * @return the long |
| 188 | + */ |
| 189 | + public long increment(String key, int by, long defaultValue, int expire) { |
| 190 | + return memcachedClient.incr(key, by, defaultValue, expire); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * 以给定的数量增加给定的键。 |
| 195 | + * |
| 196 | + * @param key the key |
| 197 | + * @param by the 增值 |
| 198 | + * @return the long |
| 199 | + */ |
| 200 | + public long increment(String key, int by) { |
| 201 | + return memcachedClient.incr(key, by); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * 减量. |
| 206 | + * |
| 207 | + * @param key the key |
| 208 | + * @param by the 减量 |
| 209 | + * @param defaultValue the 默认值(如果计数器不存在) |
| 210 | + * @param expire the 过期时间 |
| 211 | + * @return the long |
| 212 | + */ |
| 213 | + public long decrement(String key, int by, long defaultValue, int expire) { |
| 214 | + return memcachedClient.decr(key, by, defaultValue, expire); |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * 减量 |
| 219 | + * |
| 220 | + * @param key the key |
| 221 | + * @param by the 要减的值 |
| 222 | + * @return the long |
| 223 | + */ |
| 224 | + public long decrement(String key, int by) { |
| 225 | + return memcachedClient.decr(key, by); |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * 异步增量,并返回当前值. |
| 230 | + * |
| 231 | + * @param key the key |
| 232 | + * @param by the 要增加的值 |
| 233 | + * @return the long |
| 234 | + */ |
| 235 | + public Long asyncIncrement(String key, int by) { |
| 236 | + Future<Long> f = memcachedClient.asyncIncr(key, by); |
| 237 | + return getResult(f); |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * Async decrement long. |
| 242 | + * 异步减量,并返回当前值 |
| 243 | + * |
| 244 | + * @param key the key |
| 245 | + * @param by the 要减少的值 |
| 246 | + * @return the long |
| 247 | + */ |
| 248 | + public Long asyncDecrement(String key, int by) { |
| 249 | + Future<Long> f = memcachedClient.asyncDecr(key, by); |
| 250 | + return getResult(f); |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Gets result. |
| 255 | + * 获取返回结果 |
| 256 | + * |
| 257 | + * @param <T> the type parameter |
| 258 | + * @param future the future |
| 259 | + * @return the result |
| 260 | + */ |
| 261 | + public <T> T getResult(Future<T> future) { |
| 262 | + try { |
| 263 | + return future.get(DEFAULT_TIMEOUT, |
| 264 | + timeUnitSeconds); |
| 265 | + } catch (Exception e) { |
| 266 | + log.warn("获取返回结果失败!{}", e); |
| 267 | + } |
| 268 | + return null; |
| 269 | + } |
| 270 | + |
| 271 | + /** |
| 272 | + * 关闭连接 |
| 273 | + */ |
| 274 | + public void disConnect() { |
| 275 | + if (memcachedClient == null) { |
| 276 | + return; |
| 277 | + } |
| 278 | + memcachedClient.shutdown(); |
| 279 | + } |
| 280 | + |
| 281 | +} |
0 commit comments