Project

dcache

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

Development

>= 2.4.0
>= 1.3.0
>= 1.2
>= 0.5
 Project Readme

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