Redisson için açıklama şöyle
Redisson adopts the nety framework based on NIO, which can not only be used as the underlying driver client of Redis, but also can send redis commands in synchronous, asynchronous, asynchronous stream or pipeline forms, execute and process Lua scripts, and process the returned results.Maven
Şu satırı dahil ederiz
<groupId>org.redisson</groupId><artifactId>redisson</artifactId><version>3.16.2</version></dependency>
Gradle
Şu satırı dahil ederiz
dependencies {
implementation 'org.redisson:redisson:3.20.1'
}RedissonClient Sınıfı
Örnek
Şöyle yaparız
import org.redisson.Redisson
import org.redisson.api.RedissonClient
import org.redisson.config.Config
fun main() {
// Create Redisson configuration
val config = Config()
config.useSingleServer().address = "redis://127.0.0.1:6379"
// Initialize Redisson client
val redisson: RedissonClient = Redisson.create(config)
// Distributed Map Example
val users = redisson.getMap<String, String>("users")
users["john"] = "John Doe"
users["jane"] = "Jane Doe"
println("User Map: $users")
// Distributed Queue Example
val tasks = redisson.getQueue<String>("tasks")
tasks.offer("Task 1")
tasks.offer("Task 2")
tasks.offer("Task 3")
println("Queue Size: ${tasks.size}")
// Clean up
redisson.shutdown()
}RBucket Sınıfı
Örnek
Şöyle yaparız
//key value get and set
RBucket<String> nameRBucket = redisson.getBucket("username");
nameRBucket.set("lance", 60, TimeUnit.SECONDS);
redisson.getBucket("username").get();
RMap Sınıfı
Örnek
Şöyle yaparız
//operate hashes
RMap<String, String> userMap = redisson.getMap("user");
userMap.put("id", "1");
userMap.put("name", "lance");
userMap.put("age", "30");
userMap.expire(60, TimeUnit.SECONDS);
redisson.getMap("user").get("name");
//operate lists
RList<String> usernames = redisson.getList("usernames");
users.add("lance");
studentRList.expire(60, TimeUnit.SECONDS);
redisson.getList("usernames").get(0);RList Sınıfı
Örnek
Şöyle yaparız
//operate lists
RList<String> usernames = redisson.getList("usernames");
users.add("lance");
studentRList.expire(60, TimeUnit.SECONDS);
redisson.getList("usernames").get(0);RStream Sınıfı
Örnek
Bu Kotlin kodu. Şöyle yaparız
@OptIn(DelicateCoroutinesApi::class)
suspend fun redisStreamWithRedisson() {
// Create Redisson configuration
val config = Config()
config.useSingleServer().address = "redis://localhost:6379"
// Initialize Redisson client
val redisson: RedissonClient = Redisson.create(config)
// Creating a Redis Stream
val consumerGroup = "my_group"
val streamName = "my_stream"
val stream = redisson.getStream<String, String>(streamName)
// Adding messages to a Redis Stream
val job = GlobalScope.launch {
for (i in 1..10) {
val id = stream.add("key", "message $i", 10_000, true)
println("Added message $i with id $id to stream")
}
}
// Waiting for the producer to finish
job.join()
// Consuming messages from a Redis Stream
val consumerId = "my_consumer"
stream.createConsumer(consumerGroup, consumerId)
val jobs = mutableListOf<Job>()
repeat(2) {
jobs.add(GlobalScope.launch {
while (true) {
val messages = stream.readGroup(consumerGroup, consumerId, 5,
StreamMessageId.NEVER_DELIVERED)
for (message in messages) {
val messageId = message.key
println("Job $it - Received $message")
stream.ack(consumerGroup, messageId)
}
}
})
}
// Waiting for the consumers to finish
jobs.joinAll()
// Clean up
redisson.shutdown()
}
Hiç yorum yok:
Yorum Gönder