赞
踩
Redis --- Redission客户端
以下内容来自: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);
Jon Chambers edited this page 8 days ago · 32revisions
Implements RBucketand RBucketAsyncinterfaces
Redisson redisson = Redisson.create();
RBucket<AnyObject> bucket = redisson.getBucket("anyObject");
bucket.set(new AnyObject());
bucket.setAsync(new AnyObject());
AnyObject obj = bucket.get();
redisson.shutdown();
Implements RMap,RMapAsyncand ConcurrentMapinterfaces
Redisson redisson = Redisson.create();
RMap<String, SomeObject> 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<SomeObject> putAsyncFuture = map.putAsync("321");
Future<Void> fastPutAsyncFuture = map.fastPutAsync("321");
map.fastPutAsync("321", new SomeObject());
map.fastRemoveAsync("321");
redisson.shutdown();
Implements RSortedSetand SortedSetinterfaces
Redisson redisson = Redisson.create();
RSortedSet<Integer> set = redisson.getSortedSet("anySet");
set.add(3);
set.add(1);
set.add(2);
set.addAsync(5);
redisson.shutdown();
Implements RSet,RSetAsyncand Setinterfaces
Redisson redisson = Redisson.create();
RSet<SomeObject> set = redisson.getSet("anySet");
set.add(new SomeObject());
set.remove(new SomeObject());
set.addAsync(new SomeObject());
redisson.shutdown();
Implements RList,RListAsyncand Listinterfaces
Redisson redisson = Redisson.create();
RList<SomeObject> list = redisson.getList("anyList");
list.add(new SomeObject());
list.get(0);
list.remove(new SomeObject());
redisson.shutdown();
Implements RQueue,RQueueAsyncand Queueinterfaces
Redisson redisson = Redisson.create();
RQueue<SomeObject> queue = redisson.getQueue("anyQueue");
queue.add(new SomeObject());
SomeObject obj = queue.peek();
SomeObject someObj = queue.poll();
redisson.shutdown();
Implements RDeque,RDequeAsyncand Dequeinterfaces
Redisson redisson = Redisson.create();
RDeque<SomeObject> queue = redisson.getDeque("anyDeque");
queue.addFirst(new SomeObject());
queue.addLast(new SomeObject());
SomeObject obj = queue.removeFirst();
SomeObject someObj = queue.removeLast();
redisson.shutdown();
Implements RBlockingQueue,RBlockingQueueAsyncand BlockingQueueinterfaces
Redisson redisson = Redisson.create();
RBlockingQueue<SomeObject> queue = redisson.getBlockingQueue("anyQueue");
queue.offer(new SomeObject(), 12, TimeUnit.SECONDS);
SomeObject obj = queue.peek();
SomeObject someObj = queue.poll();
redisson.shutdown();
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();
Implements RAtomicLongand RAtomicLongAsyncinterfaces
Redisson redisson = Redisson.create();
RAtomicLong atomicLong = redisson.getAtomicLong("anyAtomicLong");
atomicLong.set(3);
atomicLong.incrementAndGet();
atomicLong.get();
redisson.shutdown();
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();
Implements RTopicand RTopicAsyncinterfaces
Redisson redisson = Redisson.create();
RTopic<SomeObject> topic = redisson.getTopic("anyTopic");
topic.addListener(new MessageListener<SomeObject>() {
public void onMessage(String channel, SomeObject message) {
...
}
});
// in other thread or other JVM
RTopic<SomeObject> topic = redisson.getTopic("anyTopic");
long clientsReceivedMessage = topic.publish(new SomeObject());
redisson.shutdown();
Implements RPatternTopicand RPatternTopicAsyncinterfaces
// subscribe to all topics by `topic1.*` pattern
RPatternTopicAsync<Message> topic1 = redisson.getTopicPattern("topic1.*");
int listenerId = topic1.addListener(new PatternMessageListener<Message>() {
@Override
public void onMessage(String pattern, String channel, Message msg) {
Assert.fail();
}
});
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();
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<Object> r1 = redisson.getScript().evalShaAsync(Mode.READ_ONLY,
"282297a0228f48cd3fc6a55de6316f31422f5d17",
RScript.ReturnType.VALUE, Collections.emptyList());
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<RedisConnection> 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();
long deletedObjects = redisson.delete("obj1", "obj2", "obj3");
long deletedObjectsByPattern = redisson.deleteByPattern("test?");
Queue<String> foundKeys = redisson.findKeysByPattern("name*object");
redisson.flushdb();
redisson.flushall();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。