23 Aralık 2019 Pazartesi

Redis - Redisson

Giriş
Açıklaması şöyle
Redisson is a very handy library that provides distributed Java objects and services on top of Redis.

It supports cluster, AWS Elasticache, Sentinel, master/slave and single connection modes. The Redisson project was created and is being maintained by Nikita Koksharov.
Sanırım Redis'in cluster olarak kullanımını kolaylaştırıyor.

Maven
Şu satırı dahil ederiz
<dependency>
   <groupId>org.redisson</groupId>
   <artifactId>redisson</artifactId>
   <version>3.17.6</version>
</dependency>
delayqueue Arayüzü
Açıklaması şöyle
redisson delayqueue is a delay queue implementation based on the redis zset structure. delayqueue has an ordered collection called timeoutSetName where the score of the elements is the delivery timestamp. delayqueue regularly scans for messages that have reached delivery time using zrangebyscore and then moves them to the list of ready messages.

The delayqueue guarantees that redis will not lose messages without crashing, and may be worth a try in the absence of a better solution.

With a well-designed database index, the overhead incurred by regularly scanning the database for unfinished orders is not as large as one might think. Timed task middleware such as redisson delayqueue can be used along with scanning the database as a compensation mechanism to avoid task loss due to middleware failures.
Distributed Lock
Açıklaması şöyle. Distributed Lock için bir thread çalıştırır ve bu thread TTL değerini hep ileri atar. Böylecek lock bayatlamaz. 
A more generalized solution is that once a client holds the lock, it starts a daemon thread to periodically check if the lock exists. If so, the daemon thread will reset the TTL to prevent lock auto-release. This strategy is sometimes referred to as the lease strategy, meaning that a lock is only leased to a client with a fixed lease length, and before lease expiration, client should renew the lease if the lock is still needed. For example, open source solution Redisson uses this strategy. 
...
A WatchDog daemon thread is started once a lock is acquired. This background thread periodically checks if client still holds the lock and resets the TTL accordingly. This strategy helps to prevent pre-mature lock release.
RBlockingQueue Arayüzü
ExecutorService ile birlikte kullanılabilir.

RBloomFilter Arayüzü
Bu sınıfın kardeşi RClusteredBloomFilter arayüzü. Sadece clustered ortamda kullanılabilir. Açıklaması şöyle.
Redisson also supports distributed Bloom filters in Redis via the RClusteredBloomFilter interface. The RClusteredBloomFilter is more memory-efficient, shrinking the memory used across all Redis nodes. An RClusteredBloomFilter object may contain up to 2^64 bits. Note that the  RClusteredBloomFilter is only available in Redisson's cluster mode.
contains metodu
Örnek
Şöyle yaparız.
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"));
bloomFilter.count();

RedissonSessionManager Sınıfı
org.redisson.tomcat.RedissonSessionManager sınıfı Tomcat session bilgisini Redis cluster'ında saklayabilir.

RedissonClient  Sınıfı
Örnek
Şöyle yaparız
@RequiredArgsConstructor
@Configuration
public class RedissonConfig {

  @Bean
  public RedissonClient redissionClient() {
    Config config = new Config();
    config.useSingleServer().setAddress("redis://127.0.0.1:6379");
    return Redisson.create(config);
  }
}

private final RedissonClient redisson;
RLock lock = redisson.getLock("update-stock-lock");
lock.lock(4, TimeUnit.SECONDS);
  ...
lock.unlock();

1. Asenkron Kodlama
RFuture Sınıfı
Açıklaması şöyle.
In Redisson, each asynchronous method returns an RFuture object, which represents the result of an asynchronous computation. RFuture implements the java.util.concurrent.Future and java.util.concurrent.CompletionStage interfaces in Java.

2. Reactive Kodlama
Mono Sınıfı
Açıklaması şöyle.
In Redisson, each reactive method returns a reactor.core.publisher.Mono object, which signals that it has successfully completed the computation by emitting an element.



Hiç yorum yok:

Yorum Gönder