Redis---Redissionクライアント
14350 ワード
Redis---Redissionクライアント
次の内容は次のとおりです.https://github.com/mrniko/redisson/wiki
Redisson supports follow Redis configurations: Cluster Sentinel servers Master/Slave servers Single server
Config examples
Single server connection:
//connects to default Redis server 127.0.0.1:6379
Redisson redisson = Redisson.create();
//connects to single Redis server via Config
Config config = new Config();
config.useSingleServer().setAddress("127.0.0.1:6379");
//or with database select num = 1
config.useSingleServer().setAddress("127.0.0.1:6379").setDatabse(1);
Redisson redisson = Redisson.create(config);
Master/Slave servers connection:
Config config = new Config();
config.useMasterSlaveConnection()
.setMasterAddress("127.0.0.1:6379")
.setLoadBalancer(new RandomLoadBalancer())//RoundRobinLoadBalancerused by default
.addSlaveAddress("127.0.0.1:6389", "127.0.0.1:6332","127.0.0.1:6419")
.addSlaveAddress("127.0.0.1:6399");
Redisson redisson = Redisson.create(config);
Sentinel servers connection:
Config config = new Config();
config.useSentinelConnection()
.setMasterName("mymaster")
.addSentinelAddress("127.0.0.1:26389","127.0.0.1:26379")
.addSentinelAddress("127.0.0.1:26319");
Redisson redisson = Redisson.create(config);
Cluster nodes connections:
Config config = new Config();
config.useClusterServers()
.setScanInterval(2000)//sets cluster state scan interval
.addNodeAddress("127.0.0.1:7000", "127.0.0.1:7001")
.addNodeAddress("127.0.0.1:7002");
Redisson redisson = Redisson.create(config);
Usage examples
Jon Chambers edited this page 8 days ago · 32revisions Object storage Map SortedSet Set List Queue Deque Blocking Queue Lock AtomicLong CountDownLatch Publish subscribe Publish subscribe by pattern Multiple commands batch (commands pipelining) Scripting Low level Redis client Misc operations
Object storage
Implements RBucketand RBucketAsyncinterfaces
Map
Implements RMap,RMapAsyncand ConcurrentMapinterfaces
SortedSet
Implements RSortedSetand SortedSetinterfaces
Set
Implements RSet,RSetAsyncand Setinterfaces
List
Implements RList,RListAsyncand Listinterfaces
Queue
Implements RQueue,RQueueAsyncand Queueinterfaces
Deque
Implements RDeque,RDequeAsyncand Dequeinterfaces
Blocking Queue
Implements RBlockingQueue,RBlockingQueueAsyncand BlockingQueueinterfaces
Lock
Implements RLockand Lockinterfaces
AtomicLong
Implements RAtomicLongand RAtomicLongAsyncinterfaces
CountDownLatch
Implements RCountDownLatchinterface
Publish subscribe
Implements RTopicand RTopicAsyncinterfaces
Publish subscribe by pattern
Implements RPatternTopicand RPatternTopicAsyncinterfaces
Multiple commands batch (commands pipelining)
Send multiple commands to the server without waiting forthe replies at all, and finally read the replies in a single step. Implements RBatchinterface
Scripting
Lua scripts could be executed. More details. Implements RScriptand RScriptAsyncinterfaces
Low level Redis client
Redisson uses high-perfomance async and lock-free Redisclient. You may use it if you want to do something not yet implemented byRedisson. It support both async and sync modes. Here is allcommands for client. But you may create any other command with
Misc operations
次の内容は次のとおりです.https://github.com/mrniko/redisson/wiki
Redisson supports follow Redis configurations:
Config examples
Single server connection:
//connects to default Redis server 127.0.0.1:6379
Redisson redisson = Redisson.create();
//connects to single Redis server via Config
Config config = new Config();
config.useSingleServer().setAddress("127.0.0.1:6379");
//or with database select num = 1
config.useSingleServer().setAddress("127.0.0.1:6379").setDatabse(1);
Redisson redisson = Redisson.create(config);
Master/Slave servers connection:
Config config = new Config();
config.useMasterSlaveConnection()
.setMasterAddress("127.0.0.1:6379")
.setLoadBalancer(new RandomLoadBalancer())//RoundRobinLoadBalancerused by default
.addSlaveAddress("127.0.0.1:6389", "127.0.0.1:6332","127.0.0.1:6419")
.addSlaveAddress("127.0.0.1:6399");
Redisson redisson = Redisson.create(config);
Sentinel servers connection:
Config config = new Config();
config.useSentinelConnection()
.setMasterName("mymaster")
.addSentinelAddress("127.0.0.1:26389","127.0.0.1:26379")
.addSentinelAddress("127.0.0.1:26319");
Redisson redisson = Redisson.create(config);
Cluster nodes connections:
Config config = new Config();
config.useClusterServers()
.setScanInterval(2000)//sets cluster state scan interval
.addNodeAddress("127.0.0.1:7000", "127.0.0.1:7001")
.addNodeAddress("127.0.0.1:7002");
Redisson redisson = Redisson.create(config);
Usage examples
Jon Chambers edited this page 8 days ago · 32revisions
Object storage
Implements RBucketand RBucketAsyncinterfaces
Redisson redisson = Redisson.create();
RBucket bucket = redisson.getBucket("anyObject");
bucket.set(new AnyObject());
bucket.setAsync(new AnyObject());
AnyObject obj = bucket.get();
redisson.shutdown();
Map
Implements RMap,RMapAsyncand ConcurrentMapinterfaces
Redisson redisson = Redisson.create();
RMap map = redisson.getMap("anyMap");
SomeObject prevObject = map.put("123", new SomeObject());
SomeObject currentObject = map.putIfAbsent("323", new SomeObject());
SomeObject obj = map.remove("123");
map.fastPut("321", new SomeObject());
map.fastRemove("321");
Future putAsyncFuture = map.putAsync("321");
Future fastPutAsyncFuture = map.fastPutAsync("321");
map.fastPutAsync("321", new SomeObject());
map.fastRemoveAsync("321");
redisson.shutdown();
SortedSet
Implements RSortedSetand SortedSetinterfaces
Redisson redisson = Redisson.create();
RSortedSet set = redisson.getSortedSet("anySet");
set.add(3);
set.add(1);
set.add(2);
set.addAsync(5);
redisson.shutdown();
Set
Implements RSet,RSetAsyncand Setinterfaces
Redisson redisson = Redisson.create();
RSet set = redisson.getSet("anySet");
set.add(new SomeObject());
set.remove(new SomeObject());
set.addAsync(new SomeObject());
redisson.shutdown();
List
Implements RList,RListAsyncand Listinterfaces
Redisson redisson = Redisson.create();
RList list = redisson.getList("anyList");
list.add(new SomeObject());
list.get(0);
list.remove(new SomeObject());
redisson.shutdown();
Queue
Implements RQueue,RQueueAsyncand Queueinterfaces
Redisson redisson = Redisson.create();
RQueue queue = redisson.getQueue("anyQueue");
queue.add(new SomeObject());
SomeObject obj = queue.peek();
SomeObject someObj = queue.poll();
redisson.shutdown();
Deque
Implements RDeque,RDequeAsyncand Dequeinterfaces
Redisson redisson = Redisson.create();
RDeque queue = redisson.getDeque("anyDeque");
queue.addFirst(new SomeObject());
queue.addLast(new SomeObject());
SomeObject obj = queue.removeFirst();
SomeObject someObj = queue.removeLast();
redisson.shutdown();
Blocking Queue
Implements RBlockingQueue,RBlockingQueueAsyncand BlockingQueueinterfaces
Redisson redisson = Redisson.create();
RBlockingQueue queue = redisson.getBlockingQueue("anyQueue");
queue.offer(new SomeObject(), 12, TimeUnit.SECONDS);
SomeObject obj = queue.peek();
SomeObject someObj = queue.poll();
redisson.shutdown();
Lock
Implements RLockand Lockinterfaces
Redisson redisson = Redisson.create();
RLock lock = redisson.getLock("anyLock");
lock.lock();
...
lock.unlock();
// Lock time-to-live support
// releases lock automatically after 10 seconds
// if unlock method not invoked
lock.lock(10, TimeUnit.SECONDS);
...
lock.unlock();
redisson.shutdown();
AtomicLong
Implements RAtomicLongand RAtomicLongAsyncinterfaces
Redisson redisson = Redisson.create();
RAtomicLong atomicLong = redisson.getAtomicLong("anyAtomicLong");
atomicLong.set(3);
atomicLong.incrementAndGet();
atomicLong.get();
redisson.shutdown();
CountDownLatch
Implements RCountDownLatchinterface
Redisson redisson = Redisson.create();
RCountDownLatch latch = redisson.getCountDownLatch("anyCountDownLatch");
latch.trySetCount(1);
latch.await();
// in other thread or other JVM
RCountDownLatch latch = redisson.getCountDownLatch("anyCountDownLatch");
latch.countDown();
redisson.shutdown();
Publish subscribe
Implements RTopicand RTopicAsyncinterfaces
Redisson redisson = Redisson.create();
RTopic topic = redisson.getTopic("anyTopic");
topic.addListener(new MessageListener() {
public void onMessage(String channel, SomeObject message) {
...
}
});
// in other thread or other JVM
RTopic topic = redisson.getTopic("anyTopic");
long clientsReceivedMessage = topic.publish(new SomeObject());
redisson.shutdown();
Publish subscribe by pattern
Implements RPatternTopicand RPatternTopicAsyncinterfaces
// subscribe to all topics by `topic1.*` pattern
RPatternTopicAsync topic1 = redisson.getTopicPattern("topic1.*");
int listenerId = topic1.addListener(new PatternMessageListener() {
@Override
public void onMessage(String pattern, String channel, Message msg) {
Assert.fail();
}
});
Multiple commands batch (commands pipelining)
Send multiple commands to the server without waiting forthe replies at all, and finally read the replies in a single step. Implements RBatchinterface
RBatch batch = redisson.createBatch();
batch.getMap("test").fastPutAsync("1", "2");
batch.getMap("test").fastPutAsync("2", "3");
batch.getMap("test").putAsync("2", "5");
batch.getAtomicLongAsync("counter").incrementAndGetAsync();
batch.getAtomicLongAsync("counter").incrementAndGetAsync();
List> res = batch.execute();
Scripting
Lua scripts could be executed. More details. Implements RScriptand RScriptAsyncinterfaces
redisson.getBucket("foo").set("bar");
String r = redisson.getScript().eval(Mode.READ_ONLY,
"return redis.call('get', 'foo')", RScript.ReturnType.VALUE);
// do the same using cache
RScript s = redisson.getScript();
// load script into cache to all redis master instances
String res = s.scriptLoad("return redis.call('get', 'foo')");
// res == 282297a0228f48cd3fc6a55de6316f31422f5d17
// call script by sha digest
Future
"282297a0228f48cd3fc6a55de6316f31422f5d17",
RScript.ReturnType.VALUE, Collections.emptyList());
Low level Redis client
Redisson uses high-perfomance async and lock-free Redisclient. You may use it if you want to do something not yet implemented byRedisson. It support both async and sync modes. Here is allcommands for client. But you may create any other command with
RedisCommand
object. RedisClient client = new RedisClient("localhost", 6379);
RedisConnection conn = client.connect();
//or
Future connFuture = client.connectAsync();
conn.sync(StringCodec.INSTANCE, RedisCommands.SET, "test", 0);
conn.async(StringCodec.INSTANCE, RedisCommands.GET, "test");
conn.sync(RedisCommands.PING);
conn.close()
// or
conn.closeAsync()
client.shutdown();
// or
client.shutdownAsync();
Misc operations
long deletedObjects = redisson.delete("obj1", "obj2", "obj3");
long deletedObjectsByPattern = redisson.deleteByPattern("test?");
Queue foundKeys = redisson.findKeysByPattern("name*object");
redisson.flushdb();
redisson.flushall();