use of com.lcache.core.BaseCacheExecutor in project lcache by long172066912.
the class LcacheBeanProcessor method postProcessBeforeInitialization.
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
Class<?> cls = bean.getClass();
List<Field> fieldList = new ArrayList<>();
while (!cls.equals(Object.class)) {
fieldList.addAll(new ArrayList<>(Arrays.asList(cls.getDeclaredFields())));
cls = cls.getSuperclass();
}
for (Field field : fieldList) {
if (field.isAnnotationPresent(Lcache.class)) {
field.setAccessible(true);
try {
Object fieldObj = field.get(bean);
if (fieldObj != null) {
continue;
}
Lcache annotation = field.getAnnotation(Lcache.class);
String cacheType = annotation.cacheType();
BaseCacheExecutor baseCacheExecutor = CacheClientFactory.getCacheExecutor(cacheType);
// 注入LcacheManager(使用Redisson实现的RedissonCache)
LcacheManager.addCaches(new LcacheRedissonCache(cacheType, baseCacheExecutor));
field.set(bean, baseCacheExecutor);
} catch (Exception e) {
throw new BeanCreationException(beanName, e.getMessage(), e);
}
}
}
return bean;
}
use of com.lcache.core.BaseCacheExecutor in project lcache by long172066912.
the class TestRedisCache2 method testLock.
@Test
public void testLock() {
BaseCacheExecutor baseCacheExecutor = CacheClientFactory.getCacheExecutor(CacheConfigModel.lettuce("club"));
// 多线程测试
Thread a = new Thread(() -> {
while (true) {
int i = 1;
RLock lock = baseCacheExecutor.lock("lock" + i, 1, TimeUnit.SECONDS);
try {
if (null != lock) {
System.out.println(i + "a加锁成功");
}
Thread.sleep(2000L);
System.out.println("a 执行完毕" + i);
} catch (Exception e) {
} finally {
baseCacheExecutor.unLock(lock);
System.out.println(i + "a解锁成功");
}
}
});
// 多线程测试
Thread b = new Thread(() -> {
while (true) {
int i = 1;
RLock lock = baseCacheExecutor.lock("lock" + i, 10, TimeUnit.SECONDS);
try {
if (null != lock) {
System.out.println(i + "b加锁成功");
}
Thread.sleep(500L);
System.out.println("b 执行完毕" + i);
} catch (Exception e) {
} finally {
baseCacheExecutor.unLock(lock);
System.out.println(i + "b解锁成功");
}
}
});
a.start();
b.start();
try {
a.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
use of com.lcache.core.BaseCacheExecutor in project lcache by long172066912.
the class TestRedisCache2 method testRedisson.
@Test
public void testRedisson() {
BaseCacheExecutor baseCacheExecutor = CacheClientFactory.getCacheExecutor("test", new LettuceConnectSourceConfig());
RedissonClient redissonClient = baseCacheExecutor.getRedissonClient();
// zset批量判断是否存
String zsetKey = "zset:test:1";
baseCacheExecutor.zadd(zsetKey, ImmutableMap.of("a", (double) 1, "b", (double) 2, "c", (double) 3), 3600);
System.out.println(baseCacheExecutor.zscore(zsetKey, "a"));
// redissonClient.getScoredSortedSet(zsetKey).addAll(ImmutableMap.of("d",(double) 4));
List<String> strings = Arrays.asList("a", "b", "d");
RScoredSortedSet<String> scoredSortedSet = redissonClient.getScoredSortedSet(zsetKey, StringCodec.INSTANCE);
List<Double> score = scoredSortedSet.getScore(strings);
System.out.println(JSON.toJSONString(score.stream().filter(e -> null != e).map(e -> e.toString()).collect(Collectors.toList())));
}
use of com.lcache.core.BaseCacheExecutor in project lcache by long172066912.
the class TestRedisCache2 method testList.
@Test
public void testList() {
BaseCacheExecutor baseCacheExecutor = CacheClientFactory.getCacheExecutor(CacheConfigModel.lettucePool("test"), new LettuceConnectSourceConfig());
Thread a = new Thread(() -> {
int i = 0;
while (true) {
i++;
System.out.println("Lettuce发布消息:" + i);
try {
baseCacheExecutor.lpush("testList", "test" + i, 3600);
} catch (Exception e) {
e.printStackTrace();
}
try {
Thread.sleep(1000L);
} catch (Exception e) {
e.printStackTrace();
}
}
});
Thread b = new Thread(() -> {
while (true) {
System.out.println(baseCacheExecutor.brpop(1, "testList"));
}
});
new Thread(() -> {
while (true) {
System.out.println(baseCacheExecutor.get("test"));
}
}).start();
a.start();
b.start();
try {
a.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
use of com.lcache.core.BaseCacheExecutor in project lcache by long172066912.
the class TestRedisCache2 method bzpopTest.
@Test
public void bzpopTest() {
BaseCacheExecutor baseCacheExecutor = CacheClientFactory.getCacheExecutor("test", new LettuceConnectSourceConfig());
BaseCacheExecutor baseCacheExecutor1 = CacheClientFactory.getCacheExecutor("test1", new LettuceConnectSourceConfig());
BaseCacheExecutor baseCacheExecutor2 = CacheClientFactory.getCacheExecutor("test2", new LettuceConnectSourceConfig());
String key = "bzpop:test";
baseCacheExecutor.del(key);
new Thread(() -> {
int i = 0;
while (true) {
try {
i++;
baseCacheExecutor.zadd(key, i, i + "", 3600);
Thread.sleep(10L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
while (true) {
try {
System.out.println("zpopmax : " + baseCacheExecutor1.zpopmax(key));
System.out.println("zpopmax+count : " + baseCacheExecutor1.zpopmax(key, 2));
System.out.println("bzpopmax : " + baseCacheExecutor1.bzpopmax(1, key));
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
while (true) {
try {
System.out.println("zpopmin : " + baseCacheExecutor2.zpopmin(key));
System.out.println("zpopmin+count : " + baseCacheExecutor2.zpopmin(key, 2));
System.out.println("bzpopmin : " + baseCacheExecutor2.bzpopmin(1, key));
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
while (true) {
try {
System.out.println("zcard : " + baseCacheExecutor.zcard(key));
Thread.sleep(50L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Aggregations