elephant-cache 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +24 -0
- data/README.rdoc +63 -0
- data/Rakefile +37 -0
- data/lib/elephant/cache.rb +52 -0
- data/lib/elephant.rb +1 -0
- metadata +58 -0
data/LICENSE.txt
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
Copyright (c) 2010, Jeffrey C. Reinecke
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
* Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
* Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
* Neither the name of the copyright holders nor the
|
12
|
+
names of its contributors may be used to endorse or promote products
|
13
|
+
derived from this software without specific prior written permission.
|
14
|
+
|
15
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
16
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
17
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
18
|
+
DISCLAIMED. IN NO EVENT SHALL JEFFREY REINECKE BE LIABLE FOR ANY
|
19
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
20
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
21
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
22
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
23
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
24
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
= Overview
|
2
|
+
|
3
|
+
Elephant provides a caching module that may be included into a class for easy
|
4
|
+
internal caching. The code for this is simple, but is common enough of a design
|
5
|
+
pattern that having a reusable module is handy.
|
6
|
+
|
7
|
+
Usage is quite simple:
|
8
|
+
|
9
|
+
1. include Elephant::Cache.
|
10
|
+
2. In your object's initialize method, call +initialize_cache+.
|
11
|
+
3. Create accessors for cachable quantities, using the +cache+ method.
|
12
|
+
|
13
|
+
Example:
|
14
|
+
|
15
|
+
class Point
|
16
|
+
|
17
|
+
def initialize(x=0, y=0)
|
18
|
+
initialize_cache
|
19
|
+
self.x = x
|
20
|
+
self.y = y
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_reader :x, :y
|
24
|
+
|
25
|
+
def magnitude
|
26
|
+
return cache(__method__) do
|
27
|
+
Math.sqrt(self.x**2 + self.y**2)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
Additionally, if you were to make the point mutable, you'd want to dirty the
|
34
|
+
cache like so:
|
35
|
+
|
36
|
+
def x=(x)
|
37
|
+
dirty_cache(:magnitude)
|
38
|
+
@x = x
|
39
|
+
end
|
40
|
+
|
41
|
+
def y=(y)
|
42
|
+
dirty_cache(:magnitude)
|
43
|
+
@y = y
|
44
|
+
end
|
45
|
+
|
46
|
+
Or, if you want to dirty the entire cache, call <code>dirty_cache()</code> with
|
47
|
+
no arguments.
|
48
|
+
|
49
|
+
See the Elephant::Cache module for more information.
|
50
|
+
|
51
|
+
= Contact
|
52
|
+
|
53
|
+
If you have any questions, comments, concerns, patches, or bugs, you can contact
|
54
|
+
me via the github repository at:
|
55
|
+
|
56
|
+
http://github.com/paploo/elephant
|
57
|
+
|
58
|
+
or directly via e-mail at:
|
59
|
+
|
60
|
+
mailto:jeff@paploo.net
|
61
|
+
|
62
|
+
= Version History
|
63
|
+
[1.0.0 - 2011-Aug-28] Initial release.
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require "rake/rdoctask"
|
3
|
+
|
4
|
+
# ===== RDOC BUILDING =====
|
5
|
+
# This isn't necessary if installing from a gem.
|
6
|
+
|
7
|
+
Rake::RDocTask.new do |rdoc|
|
8
|
+
rdoc.rdoc_dir = "rdoc"
|
9
|
+
rdoc.rdoc_files.add "lib/**/*.rb", "README.rdoc"
|
10
|
+
end
|
11
|
+
|
12
|
+
# ===== SPEC TESTING =====
|
13
|
+
|
14
|
+
begin
|
15
|
+
require "spec/rake/spectask"
|
16
|
+
|
17
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
18
|
+
spec.spec_opts = ['-c' '-f specdoc']
|
19
|
+
spec.spec_files = ['spec']
|
20
|
+
end
|
21
|
+
|
22
|
+
Spec::Rake::SpecTask.new(:spec_with_backtrace) do |spec|
|
23
|
+
spec.spec_opts = ['-c' '-f specdoc', '-b']
|
24
|
+
spec.spec_files = ['spec']
|
25
|
+
end
|
26
|
+
rescue LoadError
|
27
|
+
task :spec do
|
28
|
+
puts "You must have rspec installed to run this task."
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# ===== GEM BUILDING =====
|
33
|
+
|
34
|
+
desc "Build the gem file for this package"
|
35
|
+
task :build_gem do
|
36
|
+
STDOUT.puts `gem build elephant-cache.gemspec`
|
37
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Elephant
|
2
|
+
# A simple caching API.
|
3
|
+
#
|
4
|
+
# To use, initialize the cache in your object initialize method, then
|
5
|
+
# simply call <code>cache(:lookup_key){...}</code> with the block generating the value
|
6
|
+
# that should be cached. From then on, it'll use the cached value (unless
|
7
|
+
# you dirty the cache, of course).
|
8
|
+
#
|
9
|
+
# If you want to access a cached value outside of where it is created, it is
|
10
|
+
# best to cache the value in an accessor method and then call the accessor.
|
11
|
+
#
|
12
|
+
# Note: Outside of initialize_cache, the APIs are designed in a way that
|
13
|
+
# easily facilitates using any cache store, as long as it has hash-like access.
|
14
|
+
# Thus, one could make a mapper to Memcache or Mongo and override initialize_cache
|
15
|
+
# with a new implementation before calling it, and you'd be able to store anywhere.
|
16
|
+
module Cache
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
# Initializes the cache. This should be called in your object's initialize
|
21
|
+
# method. Note that this can be overriden to use any hash-like object you
|
22
|
+
# want as a cache store, allowing use of Memcache, Mongo, or SQL.
|
23
|
+
def initialize_cache
|
24
|
+
@elephant_cache = {}
|
25
|
+
end
|
26
|
+
|
27
|
+
# Retrieves the value for the given key from the cache, calculating it from
|
28
|
+
# the block if necessary.
|
29
|
+
#
|
30
|
+
# Ex:
|
31
|
+
# def foo
|
32
|
+
# return cache_value(__method__) { ...do a lot of work... }
|
33
|
+
# end
|
34
|
+
def cache_value(key)
|
35
|
+
unless(@elephant_cache.include?(key))
|
36
|
+
@elephant_cache[key] = yield if block_given?
|
37
|
+
end
|
38
|
+
return @elephant_cache[key]
|
39
|
+
end
|
40
|
+
|
41
|
+
# Given a key, it dirties the cache for that key so that the next access
|
42
|
+
# will recalculate it. If no key is given, it cleas the entire cache.
|
43
|
+
def dirty_cache(key=nil)
|
44
|
+
if(key.nil?)
|
45
|
+
@elephant_cache.clear
|
46
|
+
else
|
47
|
+
@elephant_cache.delete(key)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
data/lib/elephant.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'elephant', 'cache')
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: elephant-cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jeff Reinecke
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-08-29 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: " A simple object caching design pattern in a reusable module.\n"
|
17
|
+
email: jeff@paploo.net
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- LICENSE.txt
|
27
|
+
- Rakefile
|
28
|
+
- lib/elephant/cache.rb
|
29
|
+
- lib/elephant.rb
|
30
|
+
homepage: http://www.github.com/paploo/elephant
|
31
|
+
licenses:
|
32
|
+
- BSD
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.8.7
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.5
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: A simple object caching design pattern in a reusable module.
|
57
|
+
test_files: []
|
58
|
+
|