您的位置:首页 > 数据库 > Redis

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);

Usageexamples

JonChamberseditedthispage8daysago·
32revisions

Objectstorage
Map
SortedSet
Set
List
Queue
Deque
BlockingQueue
Lock
AtomicLong
CountDownLatch
Publishsubscribe
Publishsubscribebypattern
Multiplecommandsbatch(commandspipelining)
Scripting
LowlevelRedisclient
Miscoperations

Objectstorage

Implements
RBucketand
RBucketAsyncinterfaces

Redissonredisson=Redisson.create();


RBucket<AnyObject>bucket=redisson.getBucket("anyObject");

bucket.set(newAnyObject());

bucket.setAsync(newAnyObject());

AnyObjectobj=bucket.get();


redisson.shutdown();

Map

Implements
RMap,RMapAsyncand

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

Implements
RSortedSetand
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

Implements
RSet,RSetAsyncand

Setinterfaces

Redissonredisson=Redisson.create();


RSet<SomeObject>set=redisson.getSet("anySet");

set.add(newSomeObject());

set.remove(newSomeObject());


set.addAsync(newSomeObject());


redisson.shutdown();

List

Implements
RList,RListAsyncand

Listinterfaces

Redissonredisson=Redisson.create();


RList<SomeObject>list=redisson.getList("anyList");

list.add(newSomeObject());

list.get(0);

list.remove(newSomeObject());


redisson.shutdown();

Queue

Implements
RQueue,RQueueAsyncand

Queueinterfaces

Redissonredisson=Redisson.create();


RQueue<SomeObject>queue=redisson.getQueue("anyQueue");

queue.add(newSomeObject());

SomeObjectobj=queue.peek();

SomeObjectsomeObj=queue.poll();


redisson.shutdown();

Deque

Implements
RDeque,RDequeAsyncand

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

Implements
RBlockingQueue,RBlockingQueueAsyncand

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

Implements
RLockand
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

Implements
RAtomicLongand
RAtomicLongAsyncinterfaces

Redissonredisson=Redisson.create();


RAtomicLongatomicLong=redisson.getAtomicLong("anyAtomicLong");

atomicLong.set(3);

atomicLong.incrementAndGet();

atomicLong.get();


redisson.shutdown();

CountDownLatch

Implements
RCountDownLatchinterface

Redissonredisson=Redisson.create();


RCountDownLatchlatch=redisson.getCountDownLatch("anyCountDownLatch");

latch.trySetCount(1);

latch.await();


//inotherthreadorotherJVM

RCountDownLatchlatch=redisson.getCountDownLatch("anyCountDownLatch");

latch.countDown();


redisson.shutdown();

Publishsubscribe

Implements
RTopicand
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

Implements
RPatternTopicand
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.Implements

RBatchinterface

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.More
details.

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.Hereis

allcommandsforclient.Butyoumaycreateanyothercommandwith
RedisCommand
object.

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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: