<commit_before>= Frivol - Frivolously simple temporary storage backed by Redis
A really simple Redis-backed temporary storage mechanism intended to be used with ActiveRecord,
but will work with other ORM's or any classes really. 

I developed Frivol secifically for use in Mad Mimi (http://madmimi.com) to help with caching 
of data which requires fairly long running (multi-second) database queries, and also to help
with communication of status from background tasks running in Resque on workers to the front
end web servers. Redis was chosen because we already had Resque, which is Redis-backed. Also,
unlike memcached, Redis persists it's data to disk, meaning there is far less warmup required
when a hot system is restarted. Frivol's design is such that it solves our problem, but I 
believe it is generic enough to be used in many Rails web projects and even in other types of 
projects altogether. 

== Usage
Configure Frivol in your configuration, for example in an initializer or in environment.rb
  REDIS_CONFIG = {
    :host => "localhost", 
    :port => 6379
  }
  Frivol::Config.redis_config = REDIS_CONFIG
Now include Frivol in whichever classes you'd like to make use of temporary storage. You can optionally
call the <tt>storage_expires_in(time)</tt> class method to set a default expiry. In your methods you can 
now call the <tt>store(keys_and_values)</tt> and <tt>retrieve(keys_and_defaults)</tt> methods.

Defaults in the +retrieve+ method can be symbols, in which case Frivol will check if the class <tt>respond_to?</tt>
a method by that name to get the default.

The <tt>expire_storage(time)</tt> method can be used to set the expiry time in seconds of the temporary storage. 
The default is not to expire the storage, in which case it will live for as long as Redis keeps it.

Frivol uses the +storage_key+ method to create a base key for storage in Redis. The current implementation uses
<tt>"#{self.class.name}-#{id}"</tt> so you'll want to override that method if you have classes that don't
respond to id. 

Frivol also extends Time to allow it to be (de)serialized to JSON, which currently used to store
data in Redis.
== Example
  class BigComplexCalcer
    include Frivol
    storage_expires_in 600 # temporary storage expires in 10 minutes.

    def initialize(key)
      @key = key
    end

    def storage_key
      "frivol-test-#{key}" # override the storage key because we don't respond_to? id
    end

    def big_complex_calc
      retrieve :complex => :do_big_complex_calc # do_big_complex_calc is the method to get the default from
    end

    def last_calc_done
      last = retrieve :last => nil # default is nil
      return "never" if last.nil?
      return "#{Time.now - last} seconds ago"
    end

    def do_big_complex_calc
      # Wee! Do some really hard work here...
      # ...still working...
      store :complex => result, :last => Time.now # ...and let's keep the result for at least 10 minutes, as well as the last time we did it
    end
  end


== Note on Patches/Pull Requests
 
* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a
  future version unintentionally.
* Commit, do not mess with rakefile, version, or history.
  (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
* Send me a pull request. Bonus points for topic branches.

== Copyright

Copyright (c) 2010 <NAME>. See LICENSE for details.
<commit_msg>Small readme update<commit_after>= Frivol - Frivolously simple temporary storage backed by Redis
A really simple Redis-backed temporary storage mechanism intended to be used with ActiveRecord,
but will work with other ORM's or any classes really. 

I developed Frivol secifically for use in Mad Mimi (http://madmimi.com) to help with caching 
of data which requires fairly long running (multi-second) database queries, and also to help
with communication of status from background tasks running in Resque on workers to the front
end web servers. Redis was chosen because we already had Resque, which is Redis-backed. Also,
unlike memcached, Redis persists it's data to disk, meaning there is far less warmup required
when a hot system is restarted. Frivol's design is such that it solves our problem, but I 
believe it is generic enough to be used in many Rails web projects and even in other types of 
projects altogether. 

== Usage
Configure Frivol in your configuration, for example in an initializer or in environment.rb
  REDIS_CONFIG = {
    :host => "localhost", 
    :port => 6379
  }
  Frivol::Config.redis_config = REDIS_CONFIG
Now include Frivol in whichever classes you'd like to make use of temporary storage. You can optionally
call the <tt>storage_expires_in(time)</tt> class method to set a default expiry. In your methods you can 
now call the <tt>store(keys_and_values)</tt> and <tt>retrieve(keys_and_defaults)</tt> methods.

Defaults in the +retrieve+ method can be symbols, in which case Frivol will check if the class <tt>respond_to?</tt>
a method by that name to get the default.

The <tt>expire_storage(time)</tt> method can be used to set the expiry time in seconds of the temporary storage. 
The default is not to expire the storage, in which case it will live for as long as Redis keeps it.

Frivol uses the +storage_key+ method to create a base key for storage in Redis. The current implementation uses
<tt>"#{self.class.name}-#{id}"</tt> so you'll want to override that method if you have classes that don't
respond to id. 

Frivol also extends Time to allow it to be (de)serialized to JSON, which currently used to store
data in Redis.
== Example
  class BigComplexCalcer
    include Frivol
    storage_expires_in 600 # temporary storage expires in 10 minutes.

    def initialize(key)
      @key = key
    end

    def storage_key
      "frivol-test-#{key}" # override the storage key because we don't respond_to? id
    end

    def big_complex_calc
      retrieve :complex => :do_big_complex_calc # do_big_complex_calc is the method to get the default from
    end

    def last_calc_done
      last = retrieve :last => nil # default is nil
      return "never" if last.nil?
      return "#{Time.now - last} seconds ago"
    end

    def do_big_complex_calc
      # Wee! Do some really hard work here...
      # ...still working...
      store :complex => result, :last => Time.now # ...and let's keep the result for at least 10 minutes, as well as the last time we did it
    end
  end

Since version 0.2.0, Frivol supports buckets which each get their own storage in Redis and can have their own
expiry time. A special counter bucket stores only integer values and also has increment and decrement methods.

== Note on Patches/Pull Requests
 
* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a
  future version unintentionally.
* Commit, do not mess with rakefile, version, or history.
  (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
* Send me a pull request. Bonus points for topic branches.

== Copyright

Copyright (c) 2010 <NAME>. See LICENSE for details.

{'fraction_non_alphanumeric': 0.060543373327816855, 'fraction_numerical': 0.004551096400496483, 'mean_word_length': 3.6999351911860012, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2424038', 'n_tokens_mistral': 2091, 'n_tokens_neox': 1959, 'n_words': 1056}