项目作者: RDeckard

项目描述 :
Easy caching (in memory + files persistency) for Ruby objects
高级语言: Ruby
项目地址: git://github.com/RDeckard/fcaching.git
创建时间: 2018-08-23T15:01:07Z
项目社区:https://github.com/RDeckard/fcaching

开源协议:MIT License

下载


FCaching

Easy caching (in memory + files persistency) for Ruby objects.

Installation

Add this line to your application’s Gemfile:

  1. gem 'fcaching', github: 'RDeckard/fcaching'

And then execute:

  1. $ bundle

Usage

FCaching#fetch - the Swiss Army knife of caching

Basic usage - store and fetch:

  1. cache = FCaching.new
  2. cache.fetch("key")
  3. # => nil
  4. # Nothing is stored yet for this key
  5. cache.fetch("key") { "value" }
  6. # => "value" # The block is evaluated, its result is cached and returned
  7. cache.fetch("key")
  8. # => "value" # The cached value is returned (even after restarting the script or application)

Fetch and update basic policy:

  1. cache = FCaching.new
  2. # Store a new key/value
  3. cache.fetch("key") { {a: 1, b: 2} }
  4. # => {:a=>1, :b=>2}
  5. cache.fetch("key") { {a: 2, b: 4} }
  6. # => {:a=>1, :b=>2} # The block is not evaluated because of the existance of the cached value
  7. cache.fetch("key", force: true) { {a: 2, b: 4} }
  8. # => {:a=>2, :b=>4} # The cache is updated by force with the returned value of the block

Fetch and update policy based on time:

  1. cache = FCaching.new
  2. # Store a new key/value
  3. cache.fetch("key") { {a: 1, "b" => :value} }
  4. # => {:a=>1, "b"=>:value}
  5. sleep 2
  6. cache.fetch("key", max_age: 1)
  7. # => nil # The cache is not fetched because of its age
  8. ruby_object = Object.new
  9. # => #<Object:0x000056111cfb3d98>
  10. cache.fetch("key", max_age: 3) { ruby_object }
  11. # => {:a=>1, "b"=>:value} # The cached value is young enough so it is fetched, and the block is not evaluated
  12. sleep 2
  13. cache.fetch("key", max_age: 3) { ruby_object }
  14. # => #<Object:0x000056111cfb3d98> # The cache is updated by the block because of its outdated age (sorry dude)

Other “Low level” methods

FCaching#set and FCaching#get and FCaching#delete:

  1. cache = FCaching.new
  2. cache.get("key")
  3. # => nil
  4. cache.set("key", "nothing")
  5. # => true
  6. cache.get("key")
  7. # => "nothing"
  8. cache.set("key", "something")
  9. # => true
  10. cache.get("key")
  11. # => "something"
  12. sleep 2
  13. cache.get("key", max_age: 1)
  14. # => nil
  15. cache.get("key", max_age: 3)
  16. # => "something"
  17. cache.del("new_key")
  18. # => 0
  19. cache.del("key")
  20. # => 1
  21. cache.del("key")
  22. # => 0

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rdeckard/fcaching. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the FCaching project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.