Giter Site home page Giter Site logo

cachetier's Introduction

Cachetier

Data caching is useful when you need to invest in order to fetch the same data over and over, especially when each fetch is expensive, for example DB queries, web service calls, or the result of a non-trivial calculations.

The fastest way to cache data is local memory, but you can also use shared memory such as Redis or memcached which makes the data available to other application servers. In some cases, even a DB can be a useful cache for very long operations, or such that you can only access a limited number of times (e.g. web service API with a daily limit).

Cachetier offers a way to define several layers of cache and automatically fetch and store the data over all layers.

Usage

def MyClass 
  def self.get_value(key)
    ...
  end
    
  include Cachetier
  cachetier :get_value, { mem: { ttl: 1.minute }, redis: { ttl: 10.minutes } } 
end  

In the example above, accessing MyClass.get_value will:

  • Search a local hash for the an entry for key
  • If not found, lookup entry key in Redis
  • If not found, invoke the original get_value method, and will store the result in Redis and in the local hash.

Cachetier can also receive a block as a parameter instead of using an existing method. Cachetier will define a class method for the given name.

class MyOtherClass
  cachetier :get_other_value, { mem: { ttl: 10.sec } }, do |key|
    ...
  end
end

MyOtherClass.get_other_value(42)

Configuration

Cachetier configuration includes the layers at which data will be cached, and specific options per layer, such as TTL (time to live before expiration).

It currently defined are memory (hash) and Redis layers, but more layers (such as memcached and MongoDB) can be easily added.

You can set global default settings and override them for each method.

Cachetier.configuration = { mem: { ttl: 10.seconds } }

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

cachetier's People

Contributors

ftbl avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.