项目作者: yegor256

项目描述 :
Zero-footprint Ruby In-Memory Thread-Safe Cache
高级语言: Ruby
项目地址: git://github.com/yegor256/zache.git
创建时间: 2018-11-01T14:26:53Z
项目社区:https://github.com/yegor256/zache

开源协议:MIT License

下载


In Memory Cache for Ruby

EO principles respected here
DevOps By Rultor.com
We recommend RubyMine

rake
Gem Version
Maintainability
Yard Docs
License
Test Coverage
Hits-of-Code

This is a simple Ruby gem for in-memory caching.
Read this blog post
to understand what Zache is designed for.

First, install it:

  1. gem install zache

Then, use it like this:

  1. require 'zache'
  2. zache = Zache.new
  3. # Expires in 5 minutes
  4. v = zache.get(:count, lifetime: 5 * 60) { expensive_calculation() }

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:

  1. zache = Zache.new(sync: false)
  2. v = zache.get(:count) { expensive_calculation() }

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:

  1. zache = Zache.new(dirty: true)
  2. # Or enable dirty mode for a specific get call
  3. value = zache.get(:key, dirty: true) { expensive_calculation() }

The entire API is documented
here.
Here are some additional useful methods:

  1. # Check if a key exists
  2. zache.exists?(:key)
  3. # Remove a key
  4. zache.remove(:key)
  5. # Remove all keys
  6. zache.remove_all
  7. # Remove keys that match a condition
  8. zache.remove_by { |key| key.to_s.start_with?('temp_') }
  9. # Clean up expired keys
  10. zache.clean
  11. # Check if cache is empty
  12. zache.empty?

How to contribute

Read
these guidelines.
Make sure your build is green before you contribute
your pull request. You will need to have
Ruby 2.3+ and
Bundler installed. Then:

  1. bundle update
  2. bundle exec rake

If it’s clean and you don’t see any error messages, submit your pull request.