redis_cache_store v1.1.0

A cache store implementation that stores data in Redis

Cache::RedisCacheStore

Crystal CI GitHub release

A cache store implementation that stores data in Redis.

This shard uses jgaskins/redis as the Redis client library.

If you're looking for an implementation that uses stefanwille/crystal-redis check https://github.com/crystal-cache/redis_legacy_cache_store.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      redis_cache_store:
        github: crystal-cache/redis_cache_store
    
  2. Run shards install

Usage

require "redis_cache_store"

Redis cache values are stored as strings. Values passed to write are converted with to_s, and reads return Redis strings. Serialize complex values in your application before writing them, for example with JSON, and deserialize them after reading. Use a positive expires_in value. Redis-backed entries with zero or negative expiration are not kept.

cache = Cache::RedisCacheStore(String).new(expires_in: 1.minute, namespace: "myapp-cache")

# Fetches data from the Redis, using "myapp-cache:today" key. If there is data in
# the Redis with the given key, then that data is returned.
#
# If there is no such data in the Redis (a cache miss or expired), then
# block will be written to the Redis under the given cache key, and that
# return value will be returned.
day_of_week = cache.fetch("today") { Time.utc.day_of_week.to_s }

puts day_of_week # => Wednesday

# Store a value in the cache
cache.write("greeting", "Hello, world!")

# Retrieve the value from the cache
greeting = cache.read("greeting")
puts greeting # Output: Hello, world!

# Delete a value from the cache
cache.delete("greeting")

No namespace is set by default. Provide one if the Redis cache server is shared with other apps:

cache = Cache::RedisCacheStore(String).new(expires_in: 1.minute, namespace: "myapp-cache")

clear removes entries differently depending on namespace configuration:

  • without a namespace, clear calls FLUSHDB and removes every key in the selected Redis database
  • with a namespace, clear scans and deletes only keys matching that namespace

keys and namespaced clear use Redis SCAN internally instead of KEYS, so they avoid blocking Redis while iterating large keyspaces.

This assumes Redis was started with a default configuration, and is listening on localhost, port 6379.

You can connect to Redis by instantiating the Redis::Client class.

If you need to connect to a remote server or a different port, try:

redis_uri = URI.parse("rediss://:my-secret-pw@10.0.1.1:6380/1")
redis = Redis::Client.new(uri: redis_uri)
cache = Cache::RedisCacheStore(String).new(expires_in: 1.minute, cache: redis)

Contributing

  1. Fork it (https://github.com/crystal-cache/redis_cache_store/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

Repository

redis_cache_store

Owner
Statistic
  • 11
  • 1
  • 0
  • 1
  • 3
  • about 5 hours ago
  • January 5, 2021
License

MIT License

Links
Synced at

Fri, 19 Jun 2026 15:06:09 GMT

Languages