simple_cache 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +28 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +32 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/lib/simple_cache.rb +97 -0
- data/spec/simple_cache_spec.rb +68 -0
- data/spec/spec_helper.rb +12 -0
- metadata +140 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec", "~> 2.3.0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.5.2"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.5.2)
|
7
|
+
bundler (~> 1.0.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rake (0.8.7)
|
11
|
+
rcov (0.9.9)
|
12
|
+
rspec (2.3.0)
|
13
|
+
rspec-core (~> 2.3.0)
|
14
|
+
rspec-expectations (~> 2.3.0)
|
15
|
+
rspec-mocks (~> 2.3.0)
|
16
|
+
rspec-core (2.3.1)
|
17
|
+
rspec-expectations (2.3.0)
|
18
|
+
diff-lcs (~> 1.1.2)
|
19
|
+
rspec-mocks (2.3.0)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
bundler (~> 1.0.0)
|
26
|
+
jeweler (~> 1.5.2)
|
27
|
+
rcov
|
28
|
+
rspec (~> 2.3.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 lfborjas
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
= simple_cache
|
2
|
+
|
3
|
+
Simple cache store. For now, only a memory based solution exists.
|
4
|
+
|
5
|
+
Based on ActiveRecord::Cache
|
6
|
+
|
7
|
+
It works like a hash, but stores the last access to a key to expire it after a given time. Also
|
8
|
+
accepts a `max_size` for limiting the cache growth.
|
9
|
+
|
10
|
+
= Usage
|
11
|
+
|
12
|
+
store = SimpleCache::MemoryCache.new :timeout => 10 #seconds
|
13
|
+
store[:foo] = "bar"
|
14
|
+
puts store[:foo] #=> "bar"
|
15
|
+
sleep 10
|
16
|
+
puts store[:foo] #=> nil
|
17
|
+
|
18
|
+
== Contributing to simple_cache
|
19
|
+
|
20
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
21
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
22
|
+
* Fork the project
|
23
|
+
* Start a feature/bugfix branch
|
24
|
+
* Commit and push until you are happy with your contribution
|
25
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
26
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
27
|
+
|
28
|
+
== Copyright
|
29
|
+
|
30
|
+
Copyright (c) 2011 lfborjas. See LICENSE.txt for
|
31
|
+
further details.
|
32
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "simple_cache"
|
16
|
+
gem.homepage = "http://github.com/lfborjas/simple_cache"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{General purpose cache}
|
19
|
+
gem.description = %Q{Pure ruby caching solution, inspired by ActiveRecord::Cache}
|
20
|
+
gem.email = "luisfelipe@lfborjas.com"
|
21
|
+
gem.authors = ["lfborjas"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
25
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rspec/core'
|
30
|
+
require 'rspec/core/rake_task'
|
31
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
32
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
33
|
+
end
|
34
|
+
|
35
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
36
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
37
|
+
spec.rcov = true
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
require 'rake/rdoctask'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "simple_cache #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/simple_cache.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
=begin
|
2
|
+
Code taken mostly from ActiveRecord::Cache
|
3
|
+
Copyright (c) 2005-2011 David Heinemeier Hansson
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
=end
|
25
|
+
require 'digest/md5'
|
26
|
+
module SimpleCache
|
27
|
+
#TODO: add a super-class and implement FileSystemCache
|
28
|
+
class MemoryCache
|
29
|
+
|
30
|
+
attr_reader :cache_size
|
31
|
+
|
32
|
+
def initialize(options = {})
|
33
|
+
@cache = {}
|
34
|
+
@cache_size = 0
|
35
|
+
@key_access = {}
|
36
|
+
@max_size = options[:max_size] || 32 * 1024 #32 megabytes
|
37
|
+
@timeout = options[:timeout] || 60*30 #30 minutes
|
38
|
+
end
|
39
|
+
|
40
|
+
def encode(key)
|
41
|
+
Digest::MD5.hexdigest key
|
42
|
+
end
|
43
|
+
|
44
|
+
def delete(key)
|
45
|
+
entry = @cache.delete key
|
46
|
+
@key_access.delete key
|
47
|
+
@cache_size -= entry.size
|
48
|
+
end
|
49
|
+
|
50
|
+
def [](key)
|
51
|
+
key = encode(key)
|
52
|
+
entry = @cache[key]
|
53
|
+
if entry
|
54
|
+
@key_access[key] = Time.now.to_f
|
55
|
+
else
|
56
|
+
@key_access.delete key
|
57
|
+
end
|
58
|
+
entry
|
59
|
+
end
|
60
|
+
|
61
|
+
def []=(key, value)
|
62
|
+
key = encode(key)
|
63
|
+
old_entry = @cache[key]
|
64
|
+
@cache_size -= old_entry.size if old_entry
|
65
|
+
@key_access[key] = Time.now.to_f
|
66
|
+
@cache[key] = value
|
67
|
+
@cache_size += value.size
|
68
|
+
prune if @cache_size > @max_size
|
69
|
+
end
|
70
|
+
|
71
|
+
def has_key?(key)
|
72
|
+
!!@cache[encode(key)]
|
73
|
+
end
|
74
|
+
|
75
|
+
def prune
|
76
|
+
#first, the obvious cleanup
|
77
|
+
@cache.keys.each do |key|
|
78
|
+
delete(key) if stale? key
|
79
|
+
end
|
80
|
+
|
81
|
+
#now, try to leave it a 3/4 of it's capacity
|
82
|
+
target_size = 0.75 * @max_size
|
83
|
+
|
84
|
+
#delete the older entries
|
85
|
+
sorted_keys = @key_access.keys.sort{|a,b| @key_access[a].to_f <=> @key_access[b].to_f}
|
86
|
+
sorted_keys.each do |key|
|
87
|
+
return if @cache_size <= target_size
|
88
|
+
delete key
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def stale?(key)
|
93
|
+
(Time.now.to_f - @key_access[key]) > @timeout.to_f
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "SimpleCache" do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
@timeout = 5 #seconds
|
7
|
+
@max_size= 16 #bytes
|
8
|
+
@store = SimpleCache::MemoryCache.new :timeout => @timeout, :max_size=> @max_size
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should store a value" do
|
12
|
+
@store["foo"] = "bar"
|
13
|
+
@store.has_key?("foo").should == true
|
14
|
+
@store.cache_size.should == "bar".size
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should retrieve a value" do
|
18
|
+
@store["foo"] = "bar"
|
19
|
+
@store["foo"].should == "bar"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should assert the presence of keys" do
|
23
|
+
@store["foo"] = "bar"
|
24
|
+
@store.has_key?("foo").should == true
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should prune the cache based on size" do
|
28
|
+
@store["first"] = "z"*4
|
29
|
+
@store["second"] = "y"*4
|
30
|
+
@store["third"] = "x"*4
|
31
|
+
@store["fourth"] = "b"*4
|
32
|
+
@store["fifth"] = "a"*4
|
33
|
+
|
34
|
+
@store.cache_size.should == 12
|
35
|
+
|
36
|
+
%w{fifth fourth third}.each do |key|
|
37
|
+
@store.has_key?(key).should == true
|
38
|
+
end
|
39
|
+
|
40
|
+
%w{first second}.each do |key|
|
41
|
+
@store.has_key?(key).should == false
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should prune the cache based on time" do
|
47
|
+
@store["first"] = "z"*4
|
48
|
+
@store["second"] = "y"*4
|
49
|
+
@store["third"] = "n"*4
|
50
|
+
|
51
|
+
sleep @timeout+2
|
52
|
+
|
53
|
+
@store["third"] = "x"*4
|
54
|
+
@store["fourth"] = "a"*5
|
55
|
+
|
56
|
+
@store.cache_size.should == 9
|
57
|
+
|
58
|
+
%w{fourth third}.each do |key|
|
59
|
+
@store.has_key?(key).should == true
|
60
|
+
end
|
61
|
+
|
62
|
+
%w{first second}.each do |key|
|
63
|
+
@store.has_key?(key).should == false
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'simple_cache'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- lfborjas
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-22 00:00:00 -06:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 2
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
version: 2.3.0
|
34
|
+
name: rspec
|
35
|
+
requirement: *id001
|
36
|
+
type: :development
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 23
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 0
|
48
|
+
- 0
|
49
|
+
version: 1.0.0
|
50
|
+
name: bundler
|
51
|
+
requirement: *id002
|
52
|
+
type: :development
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
prerelease: false
|
55
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ~>
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 7
|
61
|
+
segments:
|
62
|
+
- 1
|
63
|
+
- 5
|
64
|
+
- 2
|
65
|
+
version: 1.5.2
|
66
|
+
name: jeweler
|
67
|
+
requirement: *id003
|
68
|
+
type: :development
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
prerelease: false
|
71
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
name: rcov
|
81
|
+
requirement: *id004
|
82
|
+
type: :development
|
83
|
+
description: Pure ruby caching solution, inspired by ActiveRecord::Cache
|
84
|
+
email: luisfelipe@lfborjas.com
|
85
|
+
executables: []
|
86
|
+
|
87
|
+
extensions: []
|
88
|
+
|
89
|
+
extra_rdoc_files:
|
90
|
+
- LICENSE.txt
|
91
|
+
- README.rdoc
|
92
|
+
files:
|
93
|
+
- .document
|
94
|
+
- .rspec
|
95
|
+
- Gemfile
|
96
|
+
- Gemfile.lock
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.rdoc
|
99
|
+
- Rakefile
|
100
|
+
- VERSION
|
101
|
+
- lib/simple_cache.rb
|
102
|
+
- spec/simple_cache_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
has_rdoc: true
|
105
|
+
homepage: http://github.com/lfborjas/simple_cache
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
hash: 3
|
119
|
+
segments:
|
120
|
+
- 0
|
121
|
+
version: "0"
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
hash: 3
|
128
|
+
segments:
|
129
|
+
- 0
|
130
|
+
version: "0"
|
131
|
+
requirements: []
|
132
|
+
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 1.3.7
|
135
|
+
signing_key:
|
136
|
+
specification_version: 3
|
137
|
+
summary: General purpose cache
|
138
|
+
test_files:
|
139
|
+
- spec/simple_cache_spec.rb
|
140
|
+
- spec/spec_helper.rb
|