Simple cache for Ruby services.
This is not a gem, but only one file that you can copy and use in your own project. This cache is not thread safe and is intended only for serving as a temporary memory cache for a single thread. No database or storage service is required. In-memory, no dependency, just one file of Ruby Class.
- Initialize the cache as global:
Cache = LocalCache.new
That's it! Next, you can cache result of block execution:
CACHE_KEY = 'Module::Class.calculation_result'
Cache.fetch(CACHE_KEY) do
2 + 2
end
# => 4or write/read on your own:
CACHE_KEY = 'Module::Class.calculation_result'
if Cache.valid?(CACHE_KEY)
Cache.read(CACHE_KEY)
else
Cache.write(CACHE_KEY, 5)
end
# => 5Bug reports and pull requests are welcome. This project is intended to be a safe, welcoming space for collaboration. Everyone interacting in the project codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
The gem is available as open source under the terms of the MIT License.