struct Cache::RedisStore(K, V)
- Cache::RedisStore(K, V)
- Cache::Store(K, V)
- Struct
- Value
- Object
Overview
A cache store implementation which stores data in Redis.
cache = Cache::RedisStore(String, String).new(expires_in: 1.minute)
cache.fetch("today") do
Time.now.day_of_week
end
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
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: "my-database")
cache = Cache::RedisStore(String, String).new(expires_in: 1.minute, cache: redis)
Defined in:
cache/stores/redis_store.crConstructors
Instance Method Summary
-
#fetch(key : K, *, expires_in = @expires_in, &)
Fetches data from the cache, using the given
key
. - #read(key : K)
- #write(key : K, value : V, *, expires_in = @expires_in)
Instance methods inherited from struct Cache::Store(K, V)
fetch(key : K, *, expires_in = @expires_in, &)
fetch,
initialize
initialize
Constructor methods inherited from struct Cache::Store(K, V)
new
new
Constructor Detail
Instance Method Detail
Fetches data from the cache, using the given key
. If there is data in the cache
with the given key
, then that data is returned.
If there is no such data in the cache, then a block
will be passed the key
and executed in the event of a cache miss.
Setting :expires_in
will set an expiration time on the cache.
All caches support auto-expiring content after a specified number of seconds.
This value can be specified as an option to the constructor (in which case all entries will be affected),
or it can be supplied to the #fetch
or #write
method to effect just one entry.
cache = Cache::RedisStore(String, String).new(expires_in: 1.hours)
# Set a lower value for one entry
cache.fetch("today", expires_in: 10.minutes) do
Time.now.day_of_week
end