Search in sources :

Example 11 with LettuceConnectSourceConfig

use of com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig in project lcache by long172066912.

the class HotKeyTest method test.

@Test
public void test() {
    BaseCacheExecutor baseCacheExecutor = CacheClientFactory.getCacheExecutor("friend", new LettuceConnectSourceConfig()).openLocalCache().setMonitorHotKeyStatisticCapacity(100);
    LOGGER.info("Lcache Test hotKey test begin !");
    threadPoolExecutor.execute(() -> {
        while (true) {
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            time.addAndGet(1);
            if (time.get() <= 300) {
                LOGGER.info("Lcache Test:" + num);
                System.out.println("Lcache Test:" + num);
            } else {
                LOGGER.info("Lcache Test失败次数:" + errNum);
                break;
            }
            num.set(0);
        }
    });
    final int num1 = 100;
    final int num2 = 50;
    // 数据准备
    new Thread(() -> {
        while (true) {
            System.out.println("修改数值");
            for (int j = 0; j < num2; j++) {
                baseCacheExecutor.set("test" + j, j + "", 3600);
            }
            try {
                Thread.sleep(7000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
    while (true) {
        // 10秒后,降低热key起始值
        if (time.get() == 10) {
            // System.out.println("设置热key起始值为100");
            baseCacheExecutor.setIsOpenHotKeyLocalCache(true);
            baseCacheExecutor.setMonitorHotKeyMinValue(100);
        }
        if (time.get() > 300) {
            break;
        }
        for (int i = 0; i < num1; i++) {
            try {
                threadPoolExecutor.execute(() -> {
                    for (int j = 0; j < num2; j++) {
                        try {
                            baseCacheExecutor.get("test" + j);
                        } catch (Exception e) {
                            errNum.addAndGet(1);
                        }
                        num.addAndGet(1);
                    }
                });
            } catch (Exception e) {
            }
        }
    }
    LOGGER.info("Lcache Test end !");
}
Also used : BaseCacheExecutor(com.lcache.core.BaseCacheExecutor) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) Test(org.junit.Test)

Example 12 with LettuceConnectSourceConfig

use of com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig in project lcache by long172066912.

the class TestRedisCache2 method lockNotUnlockTest.

@Test
public void lockNotUnlockTest() {
    BaseCacheExecutor baseCacheExecutor = CacheClientFactory.getCacheExecutor("test", new LettuceConnectSourceConfig());
    String lockName = "unLockTest";
    for (int i = 0; i < 5; i++) {
        int finalI = i;
        new Thread(() -> {
            try {
                RLock lock = null;
                try {
                    lock = baseCacheExecutor.lock(lockName, -1, TimeUnit.SECONDS);
                    while (true) {
                        System.out.println("持有锁" + finalI);
                        try {
                            Thread.sleep(5000L);
                            Thread.currentThread().stop();
                        } catch (InterruptedException e) {
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    baseCacheExecutor.unLock(lock);
                    System.out.println("释放锁" + finalI);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }
    while (true) {
    }
}
Also used : BaseCacheExecutor(com.lcache.core.BaseCacheExecutor) RLock(org.redisson.api.RLock) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 13 with LettuceConnectSourceConfig

use of com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig in project lcache by long172066912.

the class TestRedisCache2 method testPubSub.

@Test
public void testPubSub() {
    for (int p = 0; p < 10; p++) {
        int finalP = p;
        new Thread(() -> {
            BaseCacheExecutor baseCacheExecutor = null;
            if (finalP % 2 == 0) {
                baseCacheExecutor = CacheClientFactory.getCacheExecutor(CacheConfigModel.newCache("test"), new LettuceConnectSourceConfig());
            } else {
                baseCacheExecutor = CacheClientFactory.getCacheExecutor(CacheConfigModel.newCache("friend"));
            }
            int i = 0;
            while (true) {
                i++;
                System.out.println("Lettuce发布消息:" + i);
                try {
                    baseCacheExecutor.publish("test", "test" + i);
                    Thread.sleep(1000L);
                } catch (Exception e) {
                    e.printStackTrace();
                    StatefulConnection connectResource = (StatefulConnection) baseCacheExecutor.getConnectResource();
                    if (!connectResource.isOpen()) {
                        System.out.println("关闭连接" + connectResource);
                        baseCacheExecutor.close();
                    }
                    baseCacheExecutor.returnConnectResource();
                }
            }
        }).start();
    }
    new Thread(() -> {
        BaseCacheExecutor baseCacheExecutor = null;
        baseCacheExecutor = CacheClientFactory.getCacheExecutor(CacheConfigModel.newCache("test"), new LettuceConnectSourceConfig());
        baseCacheExecutor.subscribe((message) -> {
            System.out.println("Lettuce Local订阅消息:" + message);
        }, "test");
    }).start();
    new Thread(() -> {
        BaseCacheExecutor baseCacheExecutor = null;
        baseCacheExecutor = CacheClientFactory.getCacheExecutor(CacheConfigModel.newCache("friend"));
        baseCacheExecutor.subscribe((message) -> {
            System.out.println("Lettuce Dev订阅消息:" + message);
        }, "test");
    }).start();
    while (true) {
    }
}
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) BaseCacheExecutor(com.lcache.core.BaseCacheExecutor) StatefulConnection(io.lettuce.core.api.StatefulConnection) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 14 with LettuceConnectSourceConfig

use of com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig in project lcache by long172066912.

the class TestRedisCache2 method testLettuceCluster.

@Test
public void testLettuceCluster() {
    // LettuceClusterConnectSourceConfig lettuceClusterConnectSourceConfig = new LettuceClusterConnectSourceConfig();
    // lettuceClusterConnectSourceConfig.setNodes(new HashSet<>());
    // lettuceClusterConnectSourceConfig.getNodes().add(new LettuceConnectSourceConfig("127.0.0.1",30001,null,1500));
    // lettuceClusterConnectSourceConfig.getNodes().add(new LettuceConnectSourceConfig("127.0.0.1",30002,null,1500));
    // lettuceClusterConnectSourceConfig.getNodes().add(new LettuceConnectSourceConfig("127.0.0.1",30003,null,1500));
    // lettuceClusterConnectSourceConfig.getNodes().add(new LettuceConnectSourceConfig("127.0.0.1",30004,null,1500));
    // lettuceClusterConnectSourceConfig.getNodes().add(new LettuceConnectSourceConfig("127.0.0.1",30005,null,1500));
    // lettuceClusterConnectSourceConfig.getNodes().add(new LettuceConnectSourceConfig("127.0.0.1",30006,null,1500));
    BaseCacheExecutor baseCacheExecutor = CacheClientFactory.getCacheExecutor(CacheConfigModel.lettucePool("test"), new LettuceConnectSourceConfig());
    new Thread(() -> {
        while (true) {
            for (int i = 0; i < 100; i++) {
                try {
                    baseCacheExecutor.setex("test" + i, 86400, i + "");
                    System.out.println("写入" + i);
                    Thread.sleep(10L);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
    new Thread(() -> {
        while (true) {
            for (int i = 0; i < 100; i++) {
                try {
                    System.out.println("读取" + baseCacheExecutor.get("test" + i));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
    while (true) {
    }
}
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 15 with LettuceConnectSourceConfig

use of com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig in project lcache by long172066912.

the class TestRedisCache2 method testZaddLua.

@Test
public void testZaddLua() {
    BaseCacheExecutor baseCacheExecutor = CacheClientFactory.getCacheExecutor("test", new LettuceConnectSourceConfig());
    String key = "zaddIfKeyExistsTest";
    baseCacheExecutor.del(key);
    System.out.println(baseCacheExecutor.zaddIfKeyExists(key, 1, "a", 3600));
    System.out.println(baseCacheExecutor.zrange(key, 0, -1));
    System.out.println(baseCacheExecutor.zadd(key, 2, "b", 3600));
    System.out.println(baseCacheExecutor.zaddIfKeyExists(key, 3, "c", 3600));
    System.out.println(baseCacheExecutor.zrange(key, 0, -1));
}
Also used : BaseCacheExecutor(com.lcache.core.BaseCacheExecutor) LettuceConnectSourceConfig(com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig) Test(org.junit.Test)

Aggregations

LettuceConnectSourceConfig (com.lcache.extend.handle.redis.lettuce.config.LettuceConnectSourceConfig)17 Test (org.junit.Test)13 BaseCacheExecutor (com.lcache.core.BaseCacheExecutor)12 ExecutionException (java.util.concurrent.ExecutionException)8 ArrayList (java.util.ArrayList)6 JedisConnectSourceConfig (com.lcache.extend.handle.redis.jedis.config.JedisConnectSourceConfig)5 ImmutableMap (com.google.common.collect.ImmutableMap)3 CacheConfigModel (com.lcache.core.model.CacheConfigModel)3 PipelineCmd (com.lcache.extend.handle.pipeline.PipelineCmd)3 PipelineGet (com.lcache.extend.handle.pipeline.PipelineGet)3 PipelineZremRangeByScore (com.lcache.extend.handle.pipeline.PipelineZremRangeByScore)3 RedisURI (io.lettuce.core.RedisURI)3 RedisClusterClient (io.lettuce.core.cluster.RedisClusterClient)3 DefaultClientResources (io.lettuce.core.resource.DefaultClientResources)3 List (java.util.List)3 Map (java.util.Map)3 RLock (org.redisson.api.RLock)3 JSON (com.alibaba.fastjson.JSON)2 CacheClientFactory (com.lcache.client.CacheClientFactory)2 LcacheCaffeineLocalCache (com.lcache.core.cache.localcache.LcacheCaffeineLocalCache)2