Pages: Redis string values

Redis string values

Redis values

Besides packets, SporeRedis can store simple values in Redis. This is useful for data that several servers need to share, like cooldowns, flags, or short-lived codes.

Values are stored as strings, and every call works on the same Redis server your packets use, so SporeRedis.connect(...) must have been called first. If it hasn't, the calls throw SporeRedis is not connected.

Setting a value

SporeRedis.set("myplugin:blocked:$uuid", "true")

Add a time-to-live and the key is removed automatically when it runs out:

import kotlin.time.Duration.Companion.minutes

SporeRedis.set("myplugin:code:$code", player.uniqueId, 10.minutes)
  • The value can be any type, but it is stored with toString(). Numbers, booleans and UUIDs work fine, and you convert them back when you read them. For objects, serialize them yourself (for example with Gson) and store the JSON string.
  • The TTL is a Kotlin Duration and must be positive. It is stored with millisecond precision.
  • Setting a key that already exists overwrites it. If you don't pass a TTL, the old TTL is removed and the key no longer expires.

Setting only if it doesn't exist

setIfAbsent only writes the value when the key doesn't exist yet. It returns true if it wrote the value and false if the key was already there.

import kotlin.time.Duration.Companion.seconds

if (SporeRedis.setIfAbsent("myplugin:cooldown:report:$uuid", "1", 30.seconds)) {
    // First report, go ahead
} else {
    // Still on cooldown
}

This is a single atomic operation, so it is safe to use as a cooldown or a lock even when several servers do it at the same moment. Don't do a separate exists and set, because two servers can both pass the check.

Getting a value

val blocked = SporeRedis.get("myplugin:blocked:$uuid")

get returns the stored string, or null if the key doesn't exist or has expired.

val points = SporeRedis.get("myplugin:points:$uuid")?.toIntOrNull() ?: 0

Checking and deleting

SporeRedis.exists("myplugin:blocked:$uuid")   // true or false
SporeRedis.delete("myplugin:blocked:$uuid")   // true if the key existed

Other Redis commands

For anything beyond these helpers (counters, hashes, sets, and so on), use connection. It borrows a connection from the pool, gives it to you as a Jedis object, and returns it when the block ends:

val visits = SporeRedis.connection { jedis -> jedis.incr("myplugin:visits") }

Don't store the Jedis object or use it outside the block.

Good to know

  • The calls are blocking. They wait for Redis to answer, so avoid calling them on the main thread in hot code like events that fire constantly. Run them asynchronously instead:
    SporeCoroutines.launchAsync {
        val value = SporeRedis.get("myplugin:points:$uuid")
    }
    
  • Keys are not prefixed. SporeAPI uses your key exactly as you write it. Give your keys a prefix like plugin:feature:id so different plugins on the same Redis don't overwrite each other.
  • Errors are thrown. If Redis is unreachable, the call throws an exception, so wrap it in a try/catch if the feature must keep working without Redis.