redis操作ツール---Lettuce


以前から使われていたjedisは,Lettuceがredisを操作することもでき,スレッドの安全性と性能がjedisよりも優れていることが最近分かった.
 
Lettuce公式サイト紹介
Lettuce is a fully non-blockingRedis client built with nettyproviding Reactive, Asynchronous and Synchronous Data Access .
 
クラスコード
 
package com.hyr.lettuce.demo;

import io.lettuce.core.RedisFuture;
import io.lettuce.core.RedisURI;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.cluster.api.async.RedisAdvancedClusterAsyncCommands;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*******************************************************************************
 * @date 2018-12-11    11:59
 * @author:    
 * @Description: Lettuce  redis DEMO
 ******************************************************************************/
public class LettuceOperation {

    private static RedisClusterClient client = null;
    private static StatefulRedisClusterConnection connect;

    /**************************************
     * log message
     **************************************/
    private static Logger log = Logger.getLogger(LettuceOperation.class);

    public static Logger getLog() {
        return log;
    }

    private String resultMessage = "the method: {0} exec result:
{1}"; private String execErrorMessage = "the {0} has error."; public String getResultMessage() { return resultMessage; } public String getExecErrorMessage() { return execErrorMessage; } /*************************************/ @Before public void before() { ArrayList list = new ArrayList(); list.add(RedisURI.create("redis://192.168.0.193:7001")); list.add(RedisURI.create("redis://192.168.0.193:7002")); list.add(RedisURI.create("redis://192.168.0.193:7003")); client = RedisClusterClient.create(list); //RedisClusterClient client=RedisClusterClient.create("redis://192.168.0.193:7001"); connect = client.connect(); connect.setAutoFlushCommands(false); } /** * command:info */ @Test public void info() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.info(); loggingResultMessage(result); } /** * command:cluster_info */ @Test public void cluster_info() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.clusterInfo(); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:cluster_delete_slots */ @Test public void cluster_delete_slots() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.clusterDelSlots(5461); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:cluster_add_slots */ @Test public void cluster_add_slots() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.clusterAddSlots(5461); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:bg_rewrite_aof */ @Test public void bg_rewrite_aof() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.bgrewriteaof(); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:slave_of_no_one * ERR SLAVEOF not allowed in cluster mode. */ @Test public void slave_of_no_one() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.slaveofNoOne(); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:bg_save */ @Test public void bg_save() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.bgsave(); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:debug */ @Test public void debug() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.debugObject("a1"); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:meet */ @Test public void meet() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.clusterMeet("192.168.0.193", 7002); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:forget */ @Test public void forget() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.clusterForget("d5998dacf6d33a2a7c778d63af97bfaecd08effe"); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:dbsize */ @Test public void dbsize() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.dbsize(); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:keys */ @Test public void keys() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture> result = asyncCommands.keys("*"); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:info_section */ @Test public void info_section() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.info("memory"); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:nodes */ @Test public void nodes() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.clusterNodes(); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:ping */ @Test public void ping() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.ping(); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:flushdb */ @Test public void flushdb() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.flushdb(); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:flushall */ @Test public void flushall() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.flushall(); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:save */ @Test public void save() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.save(); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:lastsave */ @Test public void lastsave() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.lastsave(); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:random_key */ @Test public void random_key() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.randomkey(); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:dump */ @Test public void dump() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.dump("a1"); getConnect().flushCommands(); // commit; Object[] args = new Object[0]; String methodName = Thread.currentThread().getStackTrace()[1].getMethodName(); try { args = new Object[]{ methodName, new String(result.get()), }; } catch (Exception e) { loggingErrorMessage(methodName, e); } getLog().info(MessageFormat.format(getResultMessage(), args)); } /** * command:slow_log_get */ @Test public void slow_log_get() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture> result = asyncCommands.slowlogGet(); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:set */ @Test public void set() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.set("a1","hyr1"); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:get */ @Test public void get() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.get("a1"); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:hset */ @Test public void hset() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.hset("group1","key1","g1_k1_data"); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:hgetall */ @Test public void hgetall() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture> result = asyncCommands.hgetall("group1"); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:hget */ @Test public void hget() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.hget("group1","key1"); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:lpush */ @Test public void lpush() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.lpush("myqueue","data1"); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * command:lpop */ @Test public void lpop() { RedisAdvancedClusterAsyncCommands asyncCommands = getConnect().async(); RedisFuture result = asyncCommands.lpop("myqueue"); getConnect().flushCommands(); // commit; loggingResultMessage(result); } /** * logging the exec result * * @param info */ private void loggingResultMessage(RedisFuture info) { Object[] args = new Object[0]; String methodName = Thread.currentThread().getStackTrace()[2].getMethodName(); try { args = new Object[]{ methodName, info.get() }; } catch (Exception e) { loggingErrorMessage(methodName, e); } getLog().info(MessageFormat.format(getResultMessage(), args)); } public static RedisClusterClient getClient() { return client; } public static StatefulRedisClusterConnection getConnect() { return connect; } /** * logging exec error message * * @param methodName * @param e */ private void loggingErrorMessage(String methodName, Exception e) { Object[] args = { methodName }; getLog().error(MessageFormat.format(getExecErrorMessage(), args), e); } @After public void after() { connect.close(); client.shutdown(); } }

 
プロジェクトはgithubにアップロードされました.
https://github.com/huangyueranbbc/LettuceDemo