Search in sources :

Example 1 with BaseCacheExecutor

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;
}
Also used : Field(java.lang.reflect.Field) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BaseCacheExecutor(com.lcache.core.BaseCacheExecutor) ArrayList(java.util.ArrayList) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeansException(org.springframework.beans.BeansException)

Example 2 with BaseCacheExecutor

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();
    }
}
Also used : BaseCacheExecutor(com.lcache.core.BaseCacheExecutor) RLock(org.redisson.api.RLock) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 3 with BaseCacheExecutor

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())));
}
Also used : RScoredSortedSet(org.redisson.api.RScoredSortedSet) PipelineZremRangeByScore(com.lcache.extend.handle.pipeline.PipelineZremRangeByScore) Arrays(java.util.Arrays) PipelineGet(com.lcache.extend.handle.pipeline.PipelineGet) StringCodec(org.redisson.client.codec.StringCodec) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) CompletableFuture(java.util.concurrent.CompletableFuture) JedisConnectSourceConfig(com.lcache.extend.handle.redis.jedis.config.JedisConnectSourceConfig) StatefulConnection(io.lettuce.core.api.StatefulConnection) ArrayList(java.util.ArrayList) RLock(org.redisson.api.RLock) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) RedissonClient(org.redisson.api.RedissonClient) CacheClientFactory(com.lcache.client.CacheClientFactory) CacheConfigModel(com.lcache.core.model.CacheConfigModel) LcacheCaffeineLocalCache(com.lcache.core.cache.localcache.LcacheCaffeineLocalCache) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test) Collectors(java.util.stream.Collectors) PipelineCmd(com.lcache.extend.handle.pipeline.PipelineCmd) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) JSON(com.alibaba.fastjson.JSON) BaseCacheExecutor(com.lcache.core.BaseCacheExecutor) Assert.assertEquals(org.junit.Assert.assertEquals) RedissonClient(org.redisson.api.RedissonClient) BaseCacheExecutor(com.lcache.core.BaseCacheExecutor) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) Test(org.junit.Test)

Example 4 with BaseCacheExecutor

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();
    }
}
Also used : BaseCacheExecutor(com.lcache.core.BaseCacheExecutor) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 5 with BaseCacheExecutor

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();
        }
    }
}
Also used : BaseCacheExecutor(com.lcache.core.BaseCacheExecutor) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

BaseCacheExecutor (com.lcache.core.BaseCacheExecutor)32 Test (org.junit.Test)15 LettuceConnectSourceConfig (com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig)12 ExecutionException (java.util.concurrent.ExecutionException)10 ImmutableMap (com.google.common.collect.ImmutableMap)7 Map (java.util.Map)7 ArrayList (java.util.ArrayList)4 RLock (org.redisson.api.RLock)4 PipelineCmd (com.lcache.extend.handle.pipeline.PipelineCmd)3 PipelineGet (com.lcache.extend.handle.pipeline.PipelineGet)3 PipelineZremRangeByScore (com.lcache.extend.handle.pipeline.PipelineZremRangeByScore)3 JedisConnectSourceConfig (com.lcache.extend.handle.redis.jedis.config.JedisConnectSourceConfig)3 List (java.util.List)3 JSON (com.alibaba.fastjson.JSON)2 CacheClientFactory (com.lcache.client.CacheClientFactory)2 LcacheCaffeineLocalCache (com.lcache.core.cache.localcache.LcacheCaffeineLocalCache)2 CacheConfigModel (com.lcache.core.model.CacheConfigModel)2 StatefulConnection (io.lettuce.core.api.StatefulConnection)2 Arrays (java.util.Arrays)2 CompletableFuture (java.util.concurrent.CompletableFuture)2