Cache::RedisLegacyCacheStore
A cache store implementation that stores data in Redis.
This shard using stefanwille/crystal-redis as Redis client library.
If you're looking for an implementation that uses jgaskins/redis check https://github.com/crystal-cache/redis_cache_store.
Installation
-
Add the dependency to your
shard.yml
:dependencies: redis_legacy_cache_store: github: crystal-cache/redis_legacy_cache_store
-
Run
shards install
Usage
require "redis_legacy_cache_store"
It's important to note that Redis cache value must be string.
cache = Cache::RedisLegacyCacheStore(String, String).new(expires_in: 1.minute, namespace: "myapp-cache")
# => #<Cache::RedisLegacyCacheStore(String, String) redis=#<Redis::PooledClient:0x7f0f1d2cf8c0 @pool=#<ConnectionPool(Redis):0x7f0f1d2e1af0 @r=#<IO::FileDescriptor: fd=11>, @w=#<IO::FileDescriptor: fd=12>, @capacity=5, @timeout=5.0, @buffer=Bytes[0], @size=0, ending=5, @pool=[], @block=#<Proc(Redis):0x562322895dd0:closure>, @connections=nil>> expires_in=00:01:00 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.
cache.fetch("today") do
Time.utc.day_of_week
end
# => Wednesday
No namespace
is set by default. Provide one if the Redis cache
server is shared with other apps:
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
or Redis::PooledClient
class.
If you need to connect to a remote server or a different port, try:
redis = Redis.new(host: "10.0.1.1", port: 6380, password: "my-secret-pw", database: 1)
cache = Cache::RedisLegacyCacheStore(String, String).new(expires_in: 1.minute, cache: redis)
Contributing
- Fork it (https://github.com/crystal-cache/redis_legacy_cache_store/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Anton Maminov - creator and maintainer