1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| package com.ming.admin.cache;
import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import io.lettuce.core.RedisClient; import io.lettuce.core.RedisURI; import io.lettuce.core.TrackingArgs; import io.lettuce.core.api.StatefulRedisConnection; import io.lettuce.core.support.caching.CacheAccessor; import io.lettuce.core.support.caching.CacheFrontend; import io.lettuce.core.support.caching.ClientSideCaching; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test;
import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit;
@Slf4j public class RedisClientSideCachingDemoTest {
private static final String HOST = "localhost"; private static final int PORT = 6379; private static final String PASSWORD = "";
@Test public void testClient() { CacheInfo cache = getCacheInfo("m-client"); String key = "ming"; String value = "fffff"; cache.getConnection().sync().del(key); printlnCacheData(cache); log.info("获取不存在的key:{}", cache.getCacheFrontend().get(key)); printlnCacheData(cache); log.info("获取key如果不存在则加载value:{}", cache.getCacheFrontend().get(key, () -> key + ":" + value + "-" + System.currentTimeMillis())); printlnCacheData(cache); log.info("获取key:{}", cache.getCacheFrontend().get(key)); printlnCacheData(cache); }
@SneakyThrows @Test public void testMultiClient() { CacheInfo cacheInfo1 = getCacheInfo("client-1"); CacheInfo cacheInfo2 = getCacheInfo("client-2"); new Thread(() -> { for (; ; ) { cacheInfo1.getCacheFrontend().get("ming" + System.currentTimeMillis(), () -> "value" + System.currentTimeMillis()); printlnCacheData(cacheInfo1); } }).start();
new Thread(() -> { for (; ; ) { cacheInfo2.getCacheFrontend().get("ming2" + System.currentTimeMillis(), () -> "value2" + System.currentTimeMillis()); printlnCacheData(cacheInfo2); } }).start();
cacheInfo2.getConnection().sync().keys("*") .forEach(f -> cacheInfo2.getConnection().sync().del(f)); Thread.sleep(100000L); }
@Test public void testCustomLocalCache() { StatefulRedisConnection<String, String> connection = RedisClient.create(RedisURI.builder() .withHost(HOST) .withPort(PORT) .withPassword(PASSWORD.toCharArray()) .build()).connect(); MyCacheAccessor myCacheAccessor = new MyCacheAccessor(); CacheFrontend<String, String> cacheFrontend = ClientSideCaching.enable(myCacheAccessor, connection, TrackingArgs.Builder.enabled());
log.info(cacheFrontend.get("ming", () -> "value" + System.currentTimeMillis())); log.info(cacheFrontend.get("ming")); log.info(myCacheAccessor.toString()); }
public static class MyCacheAccessor implements CacheAccessor<String, String> { private static Cache<String, String> CACHE = CacheBuilder.newBuilder() .expireAfterWrite(1, TimeUnit.HOURS) .build();
@Override public String get(String key) { return CACHE.getIfPresent(key); }
@Override public void put(String key, String value) { CACHE.put(key, value); }
@Override public void evict(String key) { CACHE.invalidate(key); }
@Override public String toString() { return CACHE.stats().toString(); } }
private void printlnCacheData(CacheInfo cacheInfo) { log.info("{}-本地缓存数量:{},当前redis的缓存数量:{},\r\n---------------------------\r\n", cacheInfo.getName(), cacheInfo.getLocalMap().size(), cacheInfo.getConnection().sync().dbsize()); }
private CacheInfo getCacheInfo(String name) { StatefulRedisConnection<String, String> connection = RedisClient.create(RedisURI.builder() .withHost(HOST) .withPort(PORT) .withPassword(PASSWORD.toCharArray()) .build()).connect(); Map<String, String> map = new ConcurrentHashMap<>(); CacheFrontend<String, String> cacheFrontend = ClientSideCaching.enable(CacheAccessor.forMap(map), connection, TrackingArgs.Builder.enabled());
return new CacheInfo(name, cacheFrontend, connection, map); }
@Data @NoArgsConstructor @AllArgsConstructor public static class CacheInfo { private String name; private CacheFrontend<String, String> cacheFrontend; private StatefulRedisConnection<String, String> connection; private Map<String, String> localMap; } }
|