Cache

The Peak SDK provides a CLI for interacting with the cache service. The cache CLI allows you to perform various operations such as setting, getting, and deleting cache keys.

Basic Operations

Setting a cache key

To set a cache key with a value:

peak cache set --key "my-key" --value "my-value"

You can also set a key with a TTL (time-to-live) in seconds:

peak cache set --key "my-key" --value "my-value" --ttl 3600

Getting a cache key

To retrieve a value from the cache:

peak cache get --key "my-key"

Deleting cache keys

To delete one or more cache keys:

peak cache delete --keys "key1,key2,key3"

Checking if keys exist

To check if keys exist in the cache:

peak cache exists --keys "key1,key2,key3"

Bulk Operations

Setting multiple keys at once

To set multiple key-value pairs using a JSON mapping:

peak cache mset --mapping '{"key1": "value1", "key2": "value2", "key3": "value3"}'

Getting multiple keys at once

To retrieve multiple keys:

peak cache mget --keys "key1,key2,key3"

Redis Cluster Mode and Hash Tags

When using Redis cluster mode, keys need to hash to the same slot for bulk operations to work correctly. Use hash tags to ensure keys are processed together:

peak cache mset --mapping '{"{user}:1": "alice", "{user}:2": "bob", "{user}:session": "active"}'
peak cache mget --keys "{user}:1,{user}:2,{user}:session"

Cache Management

Flushing keys by pattern

To flush all keys matching a pattern:

peak cache flush --pattern "user:*"

Flushing all tenant keys

To flush all keys for the current tenant:

peak cache flush --all

Testing connection

To test the cache connection:

peak cache ping

TTL Operations

Setting expiration for a key

To set an expiration time for an existing key:

peak cache expire --key "my-key" --ttl 3600

Getting TTL for a key

To check the remaining time-to-live for a key:

peak cache ttl --key "my-key"

Advanced Usage

Using additional prefixes

You can set additional prefixes for your cache keys:

peak cache set --key "my-key" --value "my-value" --prefix "app-name"

JSON data handling

The cache automatically handles JSON serialization and deserialization. You can store complex data structures:

peak cache set --key "user-data" --value '{"name": "John", "age": 30, "active": true}'

When retrieving the data, it will be automatically deserialized back to its original format.