6. Distributed objects

Nikita Koksharov edited this page Nov 11, 2016 · 19 revisions

Each Redisson object bound to Redis key which means object name and can be read via getName method.

RMap map = redisson.getMap("mymap");
map.getName(); // = mymap

All operations with Redis keys extracted to RKeys interface.

RKeys keys = redisson.getKeys();

Iterable<String> allKeys = keys.getKeys();
Iterable<String> foundedKeys = keys.getKeysByPattern('key*');
long numOfDeletedKeys = keys.delete("obj1", "obj2", "obj3");
long deletedKeysAmount = keys.deleteByPattern("test?");
String randomKey = keys.randomKey();
long keysAmount = keys.count();

6.1. Object holder

Redisson distributed RBucket object for Java is an universal holder for any type of object. Maximum object size is 512MB

RBucket<AnyObject> bucket = redisson.getBucket("anyObject");
bucket.set(new AnyObject(1));
AnyObject obj = bucket.get();

bucket.trySet(new AnyObject(3));
bucket.compareAndSet(new AnyObject(4), new AnyObject(5));
bucket.getAndSet(new AnyObject(6));

6.2. Binary stream holder

Redisson provides RBinaryStream object as holder for binary stream. It exposes InputStream and OutputStream object as well. Maximum byte stream size is limited by Redis master node memory only.

RBinaryStream stream = redisson.getBinaryStream("anyStream");
byte[] content = ...

InputStream is = stream.getInputStream();
byte[] readBuffer = new byte[512];

OutputStream os = stream.getOuputStream();
byte[] contentToWrite = ...

6.3. Geospatial holder

Redisson distributed RGeo object for Java is a holder for geospatial items.

RGeo<String> geo = redisson.getGeo("test");
geo.add(new GeoEntry(13.361389, 38.115556, "Palermo"), 
        new GeoEntry(15.087269, 37.502669, "Catania"));
geo.addAsync(37.618423, 55.751244, "Moscow");

Double distance = geo.dist("Palermo", "Catania", GeoUnit.METERS);
geo.hashAsync("Palermo", "Catania");
Map<String, GeoPosition> positions = geo.pos("test2", "Palermo", "test3", "Catania", "test1");
List<String> cities = geo.radius(15, 37, 200, GeoUnit.KILOMETERS);
Map<String, GeoPosition> citiesWithPositions = geo.radiusWithPosition(15, 37, 200, GeoUnit.KILOMETERS);

6.4. BitSet

Redisson distributed BitSet object for Java has structure similar to java.util.BitSet and represents vector of bits that grows as needed. BitSet size limited by Redis to 4 294 967 295.

RBitSet set = redisson.getBitSet("simpleBitset");
set.set(0, true);
set.set(1812, false);

6.5. AtomicLong

Redisson distributed AtomicLong object for Java has structure similar to java.util.concurrent.atomic.AtomicLong object.

RAtomicLong atomicLong = redisson.getAtomicLong("myAtomicLong");

6.6. AtomicDouble

Redisson distributed AtomicDouble object for Java.

RAtomicDouble atomicDouble = redisson.getAtomicDouble("myAtomicDouble");

6.7. Topic

Redisson distributed Topic object for Java implements Publish / Subscribe mechanism.

RTopic<SomeObject> topic = redisson.getTopic("anyTopic");
topic.addListener(new MessageListener<SomeObject>() {
    public void onMessage(String channel, SomeObject message) {

// in other thread or JVM
RTopic<SomeObject> topic = redisson.getTopic("anyTopic");
long clientsReceivedMessage = topic.publish(new SomeObject());

Topic listeners will be resubscribed automatically during any Redis connection reconnection or Redis server failover.

6.7.1. Topic pattern

Redisson Topic pattern object for Java subscribes to multiple topics by specified pattern.

// subscribe to all topics by `topic1.*` pattern
RPatternTopic<Message> topic1 = redisson.getPatternTopic("topic1.*");
int listenerId = topic1.addListener(new PatternMessageListener<Message>() {
    public void onMessage(String pattern, String channel, Message msg) {

Topic pattern listeners will be resubscribed automatically during reconnection to Redis server or Redis server failover.

6.8. Bloom filter

Redis based Bloom filter for Java.

RBloomFilter<SomeObject> bloomFilter = redisson.getBloomFilter("sample");
// initialize bloom filter with 
// expectedInsertions = 55000000
// falseProbability = 0.03
bloomFilter.tryInit(55000000L, 0.03);
bloomFilter.add(new SomeObject("field1Value", "field2Value"));
bloomFilter.add(new SomeObject("field5Value", "field8Value"));
bloomFilter.contains(new SomeObject("field1Value", "field8Value"));

6.9. HyperLogLog

Redisson distributed HyperLogLog object for Java.

RHyperLogLog<Integer> log = redisson.getHyperLogLog("log");