Redis --- Redission客户端
2015-09-16 09:03
627 查看
Redis---Redission客户端
以下内容来自:https://github.com/mrniko/redisson/wiki
RedissonsupportsfollowRedisconfigurations:
Cluster
Sentinelservers
Master/Slaveservers
Singleserver
Configexamples
Singleserverconnection:
//connectstodefaultRedisserver127.0.0.1:6379
Redissonredisson=Redisson.create();
//connectstosingleRedisserverviaConfig
Configconfig=newConfig();
config.useSingleServer().setAddress("127.0.0.1:6379");
//orwithdatabaseselectnum=1
config.useSingleServer().setAddress("127.0.0.1:6379").setDatabse(1);
Redissonredisson=Redisson.create(config);
Master/Slaveserversconnection:
Configconfig=newConfig();
config.useMasterSlaveConnection()
.setMasterAddress("127.0.0.1:6379")
.setLoadBalancer(newRandomLoadBalancer())//RoundRobinLoadBalancerusedbydefault
.addSlaveAddress("127.0.0.1:6389","127.0.0.1:6332","127.0.0.1:6419")
.addSlaveAddress("127.0.0.1:6399");
Redissonredisson=Redisson.create(config);
Sentinelserversconnection:
Configconfig=newConfig();
config.useSentinelConnection()
.setMasterName("mymaster")
.addSentinelAddress("127.0.0.1:26389","127.0.0.1:26379")
.addSentinelAddress("127.0.0.1:26319");
Redissonredisson=Redisson.create(config);
Clusternodesconnections:
Configconfig=newConfig();
config.useClusterServers()
.setScanInterval(2000)//setsclusterstatescaninterval
.addNodeAddress("127.0.0.1:7000","127.0.0.1:7001")
.addNodeAddress("127.0.0.1:7002");
Redissonredisson=Redisson.create(config);
32revisions
Objectstorage
Map
SortedSet
Set
List
Queue
Deque
BlockingQueue
Lock
AtomicLong
CountDownLatch
Publishsubscribe
Publishsubscribebypattern
Multiplecommandsbatch(commandspipelining)
Scripting
LowlevelRedisclient
Miscoperations
RBucketand
RBucketAsyncinterfaces
RMap,RMapAsyncand
ConcurrentMapinterfaces
RSortedSetand
SortedSetinterfaces
RSet,RSetAsyncand
Setinterfaces
RList,RListAsyncand
Listinterfaces
RQueue,RQueueAsyncand
Queueinterfaces
RDeque,RDequeAsyncand
Dequeinterfaces
RBlockingQueue,RBlockingQueueAsyncand
BlockingQueueinterfaces
RLockand
Lockinterfaces
RAtomicLongand
RAtomicLongAsyncinterfaces
RCountDownLatchinterface
RTopicand
RTopicAsyncinterfaces
RPatternTopicand
RPatternTopicAsyncinterfaces
RBatchinterface
details.
Implements
RScriptand
RScriptAsyncinterfaces
allcommandsforclient.Butyoumaycreateanyothercommandwith
以下内容来自:https://github.com/mrniko/redisson/wiki
RedissonsupportsfollowRedisconfigurations:
Sentinelservers
Configexamples
Singleserverconnection:
//connectstodefaultRedisserver127.0.0.1:6379
Redissonredisson=Redisson.create();
//connectstosingleRedisserverviaConfig
Configconfig=newConfig();
config.useSingleServer().setAddress("127.0.0.1:6379");
//orwithdatabaseselectnum=1
config.useSingleServer().setAddress("127.0.0.1:6379").setDatabse(1);
Redissonredisson=Redisson.create(config);
Master/Slaveserversconnection:
Configconfig=newConfig();
config.useMasterSlaveConnection()
.setMasterAddress("127.0.0.1:6379")
.setLoadBalancer(newRandomLoadBalancer())//RoundRobinLoadBalancerusedbydefault
.addSlaveAddress("127.0.0.1:6389","127.0.0.1:6332","127.0.0.1:6419")
.addSlaveAddress("127.0.0.1:6399");
Redissonredisson=Redisson.create(config);
Sentinelserversconnection:
Configconfig=newConfig();
config.useSentinelConnection()
.setMasterName("mymaster")
.addSentinelAddress("127.0.0.1:26389","127.0.0.1:26379")
.addSentinelAddress("127.0.0.1:26319");
Redissonredisson=Redisson.create(config);
Clusternodesconnections:
Configconfig=newConfig();
config.useClusterServers()
.setScanInterval(2000)//setsclusterstatescaninterval
.addNodeAddress("127.0.0.1:7000","127.0.0.1:7001")
.addNodeAddress("127.0.0.1:7002");
Redissonredisson=Redisson.create(config);
Usageexamples
JonChamberseditedthispage8daysago·32revisions
Objectstorage
ImplementsRBucketand
RBucketAsyncinterfaces
Redissonredisson=Redisson.create();
RBucket<AnyObject>bucket=redisson.getBucket("anyObject");
bucket.set(newAnyObject());
bucket.setAsync(newAnyObject());
AnyObjectobj=bucket.get();
redisson.shutdown();
Map
ImplementsRMap,
ConcurrentMapinterfaces
Redissonredisson=Redisson.create();
RMap<String,SomeObject>map=redisson.getMap("anyMap");
SomeObjectprevObject=map.put("123",newSomeObject());
SomeObjectcurrentObject=map.putIfAbsent("323",newSomeObject());
SomeObjectobj=map.remove("123");
map.fastPut("321",newSomeObject());
map.fastRemove("321");
Future<SomeObject>putAsyncFuture=map.putAsync("321");
Future<Void>fastPutAsyncFuture=map.fastPutAsync("321");
map.fastPutAsync("321",newSomeObject());
map.fastRemoveAsync("321");
redisson.shutdown();
SortedSet
ImplementsRSortedSetand
SortedSetinterfaces
Redissonredisson=Redisson.create();
RSortedSet<Integer>set=redisson.getSortedSet("anySet");
set.add(3);
set.add(1);
set.add(2);
set.addAsync(5);
redisson.shutdown();
Set
ImplementsRSet,
Setinterfaces
Redissonredisson=Redisson.create();
RSet<SomeObject>set=redisson.getSet("anySet");
set.add(newSomeObject());
set.remove(newSomeObject());
set.addAsync(newSomeObject());
redisson.shutdown();
List
ImplementsRList,
Listinterfaces
Redissonredisson=Redisson.create();
RList<SomeObject>list=redisson.getList("anyList");
list.add(newSomeObject());
list.get(0);
list.remove(newSomeObject());
redisson.shutdown();
Queue
ImplementsRQueue,
Queueinterfaces
Redissonredisson=Redisson.create();
RQueue<SomeObject>queue=redisson.getQueue("anyQueue");
queue.add(newSomeObject());
SomeObjectobj=queue.peek();
SomeObjectsomeObj=queue.poll();
redisson.shutdown();
Deque
ImplementsRDeque,
Dequeinterfaces
Redissonredisson=Redisson.create();
RDeque<SomeObject>queue=redisson.getDeque("anyDeque");
queue.addFirst(newSomeObject());
queue.addLast(newSomeObject());
SomeObjectobj=queue.removeFirst();
SomeObjectsomeObj=queue.removeLast();
redisson.shutdown();
BlockingQueue
ImplementsRBlockingQueue,
BlockingQueueinterfaces
Redissonredisson=Redisson.create();
RBlockingQueue<SomeObject>queue=redisson.getBlockingQueue("anyQueue");
queue.offer(newSomeObject(),12,TimeUnit.SECONDS);
SomeObjectobj=queue.peek();
SomeObjectsomeObj=queue.poll();
redisson.shutdown();
Lock
ImplementsRLockand
Lockinterfaces
Redissonredisson=Redisson.create();
RLocklock=redisson.getLock("anyLock");
lock.lock();
...
lock.unlock();
//Locktime-to-livesupport
//releaseslockautomaticallyafter10seconds
//ifunlockmethodnotinvoked
lock.lock(10,TimeUnit.SECONDS);
...
lock.unlock();
redisson.shutdown();
AtomicLong
ImplementsRAtomicLongand
RAtomicLongAsyncinterfaces
Redissonredisson=Redisson.create();
RAtomicLongatomicLong=redisson.getAtomicLong("anyAtomicLong");
atomicLong.set(3);
atomicLong.incrementAndGet();
atomicLong.get();
redisson.shutdown();
CountDownLatch
ImplementsRCountDownLatchinterface
Redissonredisson=Redisson.create();
RCountDownLatchlatch=redisson.getCountDownLatch("anyCountDownLatch");
latch.trySetCount(1);
latch.await();
//inotherthreadorotherJVM
RCountDownLatchlatch=redisson.getCountDownLatch("anyCountDownLatch");
latch.countDown();
redisson.shutdown();
Publishsubscribe
ImplementsRTopicand
RTopicAsyncinterfaces
Redissonredisson=Redisson.create();
RTopic<SomeObject>topic=redisson.getTopic("anyTopic");
topic.addListener(newMessageListener<SomeObject>(){
publicvoidonMessage(Stringchannel,SomeObjectmessage){
...
}
});
//inotherthreadorotherJVM
RTopic<SomeObject>topic=redisson.getTopic("anyTopic");
longclientsReceivedMessage=topic.publish(newSomeObject());
redisson.shutdown();
Publishsubscribebypattern
ImplementsRPatternTopicand
RPatternTopicAsyncinterfaces
//subscribetoalltopicsby`topic1.*`pattern
RPatternTopicAsync<Message>topic1=redisson.getTopicPattern("topic1.*");
intlistenerId=topic1.addListener(newPatternMessageListener<Message>(){
@Override
publicvoidonMessage(Stringpattern,Stringchannel,Messagemsg){
Assert.fail();
}
});
Multiplecommandsbatch(commandspipelining)
Sendmultiplecommandstotheserverwithoutwaitingfortherepliesatall,andfinallyreadtherepliesinasinglestep.ImplementsRBatchinterface
RBatchbatch=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
Luascriptscouldbeexecuted.Moredetails.
Implements
RScriptand
RScriptAsyncinterfaces
redisson.getBucket("foo").set("bar");
Stringr=redisson.getScript().eval(Mode.READ_ONLY,
"returnredis.call('get','foo')",RScript.ReturnType.VALUE);
//dothesameusingcache
RScripts=redisson.getScript();
//loadscriptintocachetoallredismasterinstances
Stringres=s.scriptLoad("returnredis.call('get','foo')");
//res==282297a0228f48cd3fc6a55de6316f31422f5d17
//callscriptbyshadigest
Future<Object>r1=redisson.getScript().evalShaAsync(Mode.READ_ONLY,
"282297a0228f48cd3fc6a55de6316f31422f5d17",
RScript.ReturnType.VALUE,Collections.emptyList());
LowlevelRedisclient
Redissonuseshigh-perfomanceasyncandlock-freeRedisclient.YoumayuseitifyouwanttodosomethingnotyetimplementedbyRedisson.Itsupportbothasyncandsyncmodes.Hereisallcommandsforclient.Butyoumaycreateanyothercommandwith
RedisCommandobject.
RedisClientclient=newRedisClient("localhost",6379);
RedisConnectionconn=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();
Miscoperations
longdeletedObjects=redisson.delete("obj1","obj2","obj3");
longdeletedObjectsByPattern=redisson.deleteByPattern("test?");
Queue<String>foundKeys=redisson.findKeysByPattern("name*object");
redisson.flushdb();
redisson.flushall();
相关文章推荐
- PHP实现redis存储session
- Redis 学习笔记1
- redis增删改查封装
- Redis数据类型和抽象介绍
- Ubuntu下安装redis
- redis异常解决:attempted to load session[xxx] which has been created but not yet serialized.
- Redis的图形界面管理工具phpRedisAdmin
- [转载] Redis系统性介绍
- HBase vs. MongoDB vs. MySQL vs. Oracle vs. Redis,三大主流开源 NoSQL 数据库的 PK 两大主流传统 SQL 数据库
- HBase vs. MongoDB vs. MySQL vs. Oracle vs. Redis,三大主流开源 NoSQL 数据库的 PK 两大主流传统 SQL 数据库
- windows 下安装redis
- [转载] Redis之七种武器
- redis数据类型初探实操
- Redis调用及使用
- redis 事务
- 在Linux上rpm安装运行Redis 3.0.4
- Redis Sentinel实现Failover
- [转载] 基于Redis实现分布式消息队列
- resin的部署安装
- redis和hiredis-master使用心得