A simple caching gem.
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
ext
lib
spec
yard
.gitignore
.yardopts
History.rdoc
LICENSE
README.rdoc
Rakefile
VERSION

README.rdoc

DCache

A simple caching gem.

Features

  • Two different backends

  • Cached entries can have a timeout

  • Cache structures are either allocated at once (:ary backend) or grow/shrink dynamically (:list backend)

  • Backends are written in C for speed

Problems

  • Should use #hash for faster comparisons.

Usage

Initialize a cache using :ary backend, and storing 1000 values

require "dcache"
c = DCache.new :ary, 1000

Put some value into it:

c.add :key, "value"
c.add :key2, 34
c.add [1, 2, 3], [4, 5, 6]         # key/value can be any ruby type
c.add :timeout, "of course", 60    # will be purged after 1 minute

Retrieve values:

c.get :key                         # => "value"
c.get :not_stored                  # => nil
c.get :not_stored, "default value" # => "default_value"
c.get(:not_stored) { puts "Ouch" } # will print "Ouch"
c.get_or_add :foo, 3               # => 3
c.get :foo                         # => 3
c.get_or_raise :not_stored         # will raise ArgumentError
c.get :timeout                     # => "of course"
sleep 60
c.get :timeout                     # => nil

Remove an element:

c.delete :key
c.get :key                         # => nil
c.get :key2                        # => 34

Clear cache:

c.clear
c.get :key2                        # => nil

Get statistics:

[c.hits, c.misses]                 # => [4, 8]
# also [c.stored, c.removed] with :list backend

See {DCache} for details.

Requirements

  • Ruby 1.9 (may work with 1.8)

  • Development dependencies:

    • Jeweler

    • RSpec-1.2

    • yard (git version) (to generate docs)

Install

Install the latest release:

$ gem install dcache

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 © 2009, 2010 Kővágó, Zoltán. See {file:LICENSE} for details.