0.0
No commit activity in last 3 years
No release in over 3 years
simple object cache for ruby
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.9.2
>= 0
 Project Readme

ObjectCache

Simple object caching for ruby.

Installation

Add this line to your application's Gemfile:

gem 'object_cache'

And then execute:

$ bundle

Or install it yourself as:

$ gem install object_cache

Usage

in an initializer define ObjectCache.source "path_to_yml_file" and optional ObjectCache.ttl 5.minutes (or anything else)

From that moment on you can use ObjectCache.key to access the object you defined as "key".

The yml file looks like this:

key:
  name: ClassName
  id: 4
another_key:
  name: ClassName
  id: 5

it assumes ClassName has a method find which can be used by ClassName.find([4,5]) and returns the correct objects. (usually ActiveRecord, but can be used for mongoid objects, etc.)

Class cache

Class cache uses the ObjectCache::ClassCacher.cache("Foo", [1,2,3]) syntax to store Foo objects with ids 1,2,3 in cache (they can then be accessed in any permutation via ObjectCache::ClassCacher.cache("Foo", 1)

We recommend doing something like this to make the implementation easier:

class Foo < ActiveRecord::Base

  def self.cache(ids=[])
    ObjectCache::ClassCacher.cache("Foo", ids)
  end

end

and from this point simply use object_cache by:

Foo.cache(1)
Foo.cache(1,2,3)
or
Foo.cache([1,2,3])

Cache Stores

by default, ObjectCache stores the cached objects in memory, using the ObjectCache::MemoryStore class. The current supported stores are memory store and rails store which uses Rails' abstraction of cache store to store the objects.

in the object_cache initializer define the cache store by doing this:

require "object_cache/rails_store"
ObjectCache.cache_store = ObjectCache::RailsStore

The rails cache store assumes that Rails.cache is defined, and will use whatever cache store you're using in your rails application.

This can be easily customizable, and you can write your own cache store if you like (feel free to contribute)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request