https://github.com/yegor256/zache
Zero-footprint Ruby In-Memory Thread-Safe Cache: when a naive implementation is enough
https://github.com/yegor256/zache
cache data-structures memory-management ruby ruby-gem
Last synced: 23 days ago
JSON representation
Zero-footprint Ruby In-Memory Thread-Safe Cache: when a naive implementation is enough
- Host: GitHub
- URL: https://github.com/yegor256/zache
- Owner: yegor256
- License: mit
- Created: 2018-11-01T14:26:53.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-04-07T23:50:10.000Z (25 days ago)
- Last Synced: 2025-04-09T21:18:15.968Z (23 days ago)
- Topics: cache, data-structures, memory-management, ruby, ruby-gem
- Language: Ruby
- Homepage: https://www.yegor256.com/2019/02/05/zache.html
- Size: 355 KB
- Stars: 47
- Watchers: 3
- Forks: 6
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# In Memory Cache for Ruby
[](https://www.elegantobjects.org)
[](https://www.rultor.com/p/yegor256/zache)
[](https://www.jetbrains.com/ruby/)[](https://github.com/yegor256/zache/actions/workflows/rake.yml)
[](https://badge.fury.io/rb/zache)
[](https://codeclimate.com/github/yegor256/zache/maintainability)
[](https://rubydoc.info/github/yegor256/zache/master/frames)
[](https://github.com/yegor256/zache/blob/master/LICENSE.txt)
[](https://codecov.io/github/yegor256/zache?branch=master)
[](https://hitsofcode.com/view/github/yegor256/zache)This is a simple Ruby gem for in-memory caching.
Read [this blog post](https://www.yegor256.com/2019/02/05/zache.html)
to understand what Zache is designed for.First, install it:
```bash
gem install zache
```Then, use it like this:
```ruby
require 'zache'
zache = Zache.new
# Expires in 5 minutes
v = zache.get(:count, lifetime: 5 * 60) { expensive() }
```If you omit the `lifetime` parameter, the key will never expire.
By default `Zache` is thread-safe. It locks the entire cache on each
`get` call. You can turn that off by using the `sync` argument:```ruby
zache = Zache.new(sync: false)
v = zache.get(:count) { expensive() }
```You may use "dirty" mode, which will return an expired value while
calculation is in progress. For example, if you have a value in the cache that's
expired, and you call `get` with a long-running block, the thread waits.
If another thread calls `get` again, that second thread won't wait, but will
receive the expired value from the cache. This is a very convenient mode for situations
where absolute data accuracy is less important than performance.The entire API is documented
[here](https://www.rubydoc.info/github/yegor256/zache/master/Zache)
(there are many other convenient methods).## How to contribute
Read
[these guidelines](https://www.yegor256.com/2014/04/15/github-guidelines.html).
Make sure your build is green before you contribute
your pull request. You will need to have
[Ruby](https://www.ruby-lang.org/en/) 2.3+ and
[Bundler](https://bundler.io/) installed. Then:```bash
bundle update
bundle exec rake
```If it's clean and you don't see any error messages, submit your pull request.