|
| 1 | +/* |
| 2 | + * Copyright 2011-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.hsweb.concureent.cache.monitor; |
| 18 | + |
| 19 | +import static org.springframework.util.Assert.*; |
| 20 | +import static org.springframework.util.ObjectUtils.*; |
| 21 | + |
| 22 | +import java.lang.reflect.Constructor; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.concurrent.Callable; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +import org.hsweb.commons.StringUtils; |
| 29 | +import org.hsweb.web.core.cache.monitor.MonitorCache; |
| 30 | +import org.hsweb.web.core.utils.ThreadLocalUtils; |
| 31 | +import org.springframework.cache.Cache; |
| 32 | +import org.springframework.cache.support.SimpleValueWrapper; |
| 33 | +import org.springframework.dao.DataAccessException; |
| 34 | +import org.springframework.data.redis.RedisSystemException; |
| 35 | +import org.springframework.data.redis.cache.RedisCache; |
| 36 | +import org.springframework.data.redis.cache.RedisCacheElement; |
| 37 | +import org.springframework.data.redis.cache.RedisCacheKey; |
| 38 | +import org.springframework.data.redis.connection.RedisConnection; |
| 39 | +import org.springframework.data.redis.connection.ReturnType; |
| 40 | +import org.springframework.data.redis.core.RedisCallback; |
| 41 | +import org.springframework.data.redis.core.RedisOperations; |
| 42 | +import org.springframework.data.redis.serializer.RedisSerializer; |
| 43 | +import org.springframework.data.redis.serializer.StringRedisSerializer; |
| 44 | +import org.springframework.util.ClassUtils; |
| 45 | + |
| 46 | +/** |
| 47 | + * Cache implementation on top of Redis. |
| 48 | + * |
| 49 | + * @author Costin Leau |
| 50 | + * @author Christoph Strobl |
| 51 | + * @author Thomas Darimont |
| 52 | + */ |
| 53 | +@SuppressWarnings("unchecked") |
| 54 | +public class RedisMonitorCache extends RedisCache implements Cache, MonitorCache { |
| 55 | + |
| 56 | + @SuppressWarnings("rawtypes")// |
| 57 | + private final RedisOperations redisOperations; |
| 58 | + private final byte[] totalTimeKey; |
| 59 | + private final byte[] hitTimeKey; |
| 60 | + private final byte[] putTimeKey; |
| 61 | + private final byte[] keySetKey; |
| 62 | + private long expiration = 0; |
| 63 | + |
| 64 | + public RedisMonitorCache(String name, byte[] prefix, RedisOperations<? extends Object, ? extends Object> redisOperations, |
| 65 | + long expiration) { |
| 66 | + super(name, prefix, redisOperations, expiration); |
| 67 | + this.expiration = expiration; |
| 68 | + this.redisOperations = redisOperations; |
| 69 | + this.keySetKey = (name + "~keys").getBytes(); |
| 70 | + this.totalTimeKey = name.concat(":total-times").getBytes(); |
| 71 | + this.hitTimeKey = name.concat(":hit-times").getBytes(); |
| 72 | + this.putTimeKey = name.concat(":put-times").getBytes(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public <T> T get(Object key, Class<T> type) { |
| 77 | + String localCacheKey = "cache-".concat(String.valueOf(key)); |
| 78 | + T localCache = ThreadLocalUtils.get(localCacheKey); |
| 79 | + if (localCache != null) { |
| 80 | + return localCache; |
| 81 | + } |
| 82 | + T v = super.get(key, type); |
| 83 | + redisOperations.execute((RedisCallback) connection -> { |
| 84 | + connection.incr(totalTimeKey); |
| 85 | + if (v != null) { |
| 86 | + connection.incr(hitTimeKey); |
| 87 | + } |
| 88 | + return null; |
| 89 | + }); |
| 90 | + if (v != null) { |
| 91 | + ThreadLocalUtils.put(localCacheKey, v); |
| 92 | + } |
| 93 | + return v; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public ValueWrapper get(Object key) { |
| 98 | + String localCacheKey = "cache-".concat(String.valueOf(key)); |
| 99 | + ValueWrapper localCache = ThreadLocalUtils.get(localCacheKey); |
| 100 | + if (localCache != null) { |
| 101 | + return localCache; |
| 102 | + } |
| 103 | + ValueWrapper wrapper = super.get(key); |
| 104 | + redisOperations.execute((RedisCallback) connection -> { |
| 105 | + connection.incr(totalTimeKey); |
| 106 | + if (wrapper != null) { |
| 107 | + connection.incr(hitTimeKey); |
| 108 | + } |
| 109 | + return null; |
| 110 | + }); |
| 111 | + if (wrapper != null) { |
| 112 | + ThreadLocalUtils.put(localCacheKey, wrapper); |
| 113 | + } |
| 114 | + return wrapper; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void put(Object key, Object value) { |
| 119 | + super.put(key, value); |
| 120 | + redisOperations.execute((RedisCallback) connection -> { |
| 121 | + connection.multi(); |
| 122 | + connection.incr(putTimeKey); |
| 123 | + connection.sAdd(keySetKey, ((String) key).getBytes()); |
| 124 | + if (expiration != 0) connection.expire(keySetKey, expiration); |
| 125 | + connection.exec(); |
| 126 | + return null; |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void evict(Object key) { |
| 132 | + super.evict(key); |
| 133 | + redisOperations.execute((RedisCallback) connection -> { |
| 134 | + connection.sRem(keySetKey, ((String) key).getBytes()); |
| 135 | + return null; |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void clear() { |
| 141 | + super.clear(); |
| 142 | + redisOperations.delete(keySetKey); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public Set<Object> keySet() { |
| 147 | + return (Set<Object>) redisOperations.execute((RedisCallback) connection -> connection.sMembers(keySetKey).stream().map(String::new).collect(Collectors.toSet())); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public int size() { |
| 152 | + return redisOperations.opsForSet().size(new String(keySetKey)).intValue(); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public long getTotalTimes() { |
| 157 | + return StringUtils.toInt(redisOperations.opsForValue().get(new String(totalTimeKey))); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public long getHitTimes() { |
| 162 | + return StringUtils.toInt(redisOperations.opsForValue().get(new String(hitTimeKey))); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public long getPutTimes() { |
| 167 | + return StringUtils.toInt(redisOperations.opsForValue().get(new String(putTimeKey))); |
| 168 | + } |
| 169 | +} |
0 commit comments