Search in sources :

Example 1 with RedisPubSubCommands

use of io.lettuce.core.pubsub.api.sync.RedisPubSubCommands in project sandbox by backpaper0.

the class DelExpiredEventDemo method main.

public static void main(String[] args) throws Exception {
    RedisClient redisClient = RedisClient.create("redis://localhost:6379");
    CountDownLatch ready = new CountDownLatch(1);
    CountDownLatch shutdown = new CountDownLatch(1);
    new Thread(() -> {
        CountDownLatch latch = new CountDownLatch(3);
        try (StatefulRedisPubSubConnection<String, String> con = redisClient.connectPubSub()) {
            con.addListener(new RedisPubSubAdapter<>() {

                @Override
                public void message(String pattern, String channel, String message) {
                    System.out.printf("pattern=%s, channel=%s, message=%s%n", pattern, channel, message);
                    latch.countDown();
                }
            });
            RedisPubSubCommands<String, String> commands = con.sync();
            commands.psubscribe("__keyevent@*__:del", "__keyevent@*__:expired");
            ready.countDown();
            try {
                latch.await(1, TimeUnit.MINUTES);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            shutdown.countDown();
        }
    }).start();
    ready.await(1, TimeUnit.MINUTES);
    try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
        RedisCommands<String, String> commands = connection.sync();
        commands.set("foo", "Hello, world!");
        commands.del("foo");
        commands.setex("bar", 1, "Hello, world!");
        commands.set("baz", "Hello, world!");
        commands.expire("baz", 2);
    }
    shutdown.await(1, TimeUnit.MINUTES);
}
Also used : RedisClient(io.lettuce.core.RedisClient) RedisPubSubAdapter(io.lettuce.core.pubsub.RedisPubSubAdapter) RedisPubSubCommands(io.lettuce.core.pubsub.api.sync.RedisPubSubCommands) CountDownLatch(java.util.concurrent.CountDownLatch) StatefulRedisPubSubConnection(io.lettuce.core.pubsub.StatefulRedisPubSubConnection)

Aggregations

RedisClient (io.lettuce.core.RedisClient)1 RedisPubSubAdapter (io.lettuce.core.pubsub.RedisPubSubAdapter)1 StatefulRedisPubSubConnection (io.lettuce.core.pubsub.StatefulRedisPubSubConnection)1 RedisPubSubCommands (io.lettuce.core.pubsub.api.sync.RedisPubSubCommands)1 CountDownLatch (java.util.concurrent.CountDownLatch)1