simple_cache_rs 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ gem "sqlite3"
3
+
4
+ # Add dependencies to develop your gem here.
5
+ # Include everything needed to run rake, tests, features, etc.
6
+ group :development do
7
+ gem "bundler"
8
+ gem "jeweler"
9
+ gem 'simplecov', :require => false
10
+ gem "ruby-debug19"
11
+ gem "timecop"
12
+ gem "redis"
13
+ end
14
+
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ archive-tar-minitar (0.5.2)
5
+ columnize (0.3.6)
6
+ git (1.2.5)
7
+ jeweler (1.8.3)
8
+ bundler (~> 1.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rdoc
12
+ json (1.6.6)
13
+ linecache19 (0.5.12)
14
+ ruby_core_source (>= 0.1.4)
15
+ multi_json (1.2.0)
16
+ rake (0.9.2.2)
17
+ rdoc (3.12)
18
+ json (~> 1.4)
19
+ redis (2.2.2)
20
+ ruby-debug-base19 (0.11.25)
21
+ columnize (>= 0.3.1)
22
+ linecache19 (>= 0.5.11)
23
+ ruby_core_source (>= 0.1.4)
24
+ ruby-debug19 (0.11.6)
25
+ columnize (>= 0.3.1)
26
+ linecache19 (>= 0.5.11)
27
+ ruby-debug-base19 (>= 0.11.19)
28
+ ruby_core_source (0.1.5)
29
+ archive-tar-minitar (>= 0.5.2)
30
+ simplecov (0.6.1)
31
+ multi_json (~> 1.0)
32
+ simplecov-html (~> 0.5.3)
33
+ simplecov-html (0.5.3)
34
+ sqlite3 (1.3.5)
35
+ timecop (0.3.5)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ bundler
42
+ jeweler
43
+ redis
44
+ ruby-debug19
45
+ simplecov
46
+ sqlite3
47
+ timecop
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 radiospiel
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,19 @@
1
+ = simple_cache
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to simple_cache
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * 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.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 radiospiel. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "simple_cache_rs"
18
+ gem.homepage = "http://github.com/radiospiel/simple_cache"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{You need a cache? Just throw me in.}
21
+ gem.description = %Q{A sensibly fast, yet simplistic file based cache.}
22
+ gem.email = "eno@open-lab.org"
23
+ gem.authors = ["radiospiel"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "micro_sql #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.9.1
@@ -0,0 +1,19 @@
1
+ require "base64"
2
+ require "digest/md5"
3
+
4
+ module SimpleCache::Marshal
5
+ extend self
6
+
7
+ def uid(key)
8
+ md5 = Digest::MD5.hexdigest(key)
9
+ md5.unpack("LL").inject { |a,b| (a << 31) + b }
10
+ end
11
+
12
+ def unmarshal(marshalled)
13
+ ::Marshal.load Base64.decode64(marshalled) if marshalled
14
+ end
15
+
16
+ def marshal(value)
17
+ Base64.encode64 ::Marshal.dump(value)
18
+ end
19
+ end
@@ -0,0 +1,32 @@
1
+ require "redis"
2
+
3
+ class SimpleCache::RedisStore
4
+ Marshal = ::SimpleCache::Marshal
5
+
6
+ def initialize(url)
7
+ @redis = Redis.connect(:url => url)
8
+ end
9
+
10
+ def clear
11
+ @redis.flushdb
12
+ end
13
+
14
+ def fetch(key, &block)
15
+ if value = @redis.get(Marshal.uid(key))
16
+ Marshal.unmarshal(value)
17
+ elsif block_given?
18
+ yield self, key
19
+ end
20
+ end
21
+
22
+ def store(key, value, max_age = nil)
23
+ cache_id = Marshal.uid(key)
24
+ if value
25
+ @redis.set cache_id, Marshal.marshal(value)
26
+ @redis.expire cache_id, max_age if max_age
27
+ else
28
+ @redis.del cache_id
29
+ end
30
+ value
31
+ end
32
+ end
@@ -0,0 +1,72 @@
1
+ require "sqlite3"
2
+
3
+ # A simplistic Sqlite database interface
4
+ class SimpleCache::SqliteDatabase
5
+ def initialize(path)
6
+ FileUtils.mkdir_p File.dirname(@path)
7
+
8
+ @impl = SQLite3::Database.new(path)
9
+ @prepared_queries = {}
10
+ ask "PRAGMA synchronous = OFF"
11
+ end
12
+
13
+ def exec(sql, *args)
14
+ query = @prepared_queries[sql] ||= @impl.prepare(sql)
15
+ query.execute!(*args)
16
+ end
17
+
18
+ def ask(sql, *args)
19
+ exec(sql, *args).first
20
+ end
21
+ end
22
+
23
+ class SimpleCache::SqliteStore < SimpleCache::SqliteDatabase
24
+ Marshal = ::SimpleCache::Marshal
25
+
26
+ TABLE_NAME = "simple_cache"
27
+
28
+ def self.base_dir
29
+ if RUBY_PLATFORM.downcase.include?("darwin")
30
+ "#{Dir.home}/Library/Cache"
31
+ else
32
+ "#{Dir.home}/cache"
33
+ end
34
+ end
35
+
36
+ attr :path
37
+
38
+ def initialize(name)
39
+ @path = "#{SimpleCache::SqliteStore.base_dir}/#{name}/simple_cache.sqlite3"
40
+ super @path
41
+
42
+ begin
43
+ ask("SELECT 1 FROM #{TABLE_NAME} LIMIT 1")
44
+ rescue SQLite3::SQLException
45
+ ask("CREATE TABLE #{TABLE_NAME}(uid INTEGER PRIMARY KEY, value TEXT, ttl INTEGER NOT NULL)")
46
+ end
47
+ end
48
+
49
+ def fetch(key, &block)
50
+ value, ttl = ask("SELECT value, ttl FROM #{TABLE_NAME} WHERE uid=?", Marshal.uid(key))
51
+ if ttl && (ttl == 0 || ttl > Time.now.to_i)
52
+ Marshal.unmarshal(value)
53
+ elsif block_given?
54
+ yield self, key
55
+ end
56
+ end
57
+
58
+ def store(key, value, ttl = nil)
59
+ unless value.nil?
60
+ ask("REPLACE INTO #{TABLE_NAME}(uid, value, ttl) VALUES(?,?,?)",
61
+ Marshal.uid(key), Marshal.marshal(value), ttl ? ttl + Time.now.to_i : 0)
62
+ else
63
+ ask("DELETE FROM #{TABLE_NAME} WHERE uid=?", Marshal.uid(key))
64
+ end
65
+
66
+ value
67
+ end
68
+
69
+ def clear
70
+ ask "DELETE FROM #{TABLE_NAME}"
71
+ end
72
+ end
@@ -0,0 +1,43 @@
1
+ require "uri"
2
+
3
+ module SimpleCache
4
+ end
5
+
6
+ require_relative "simple_cache/marshal"
7
+ require_relative "simple_cache/sqlite_store"
8
+
9
+ module SimpleCache
10
+ def self.new(url)
11
+ uri = URI.parse(url)
12
+
13
+ cache = case uri.scheme
14
+ when "redis" then
15
+ require_relative "simple_cache/redis_store"
16
+ SimpleCache::RedisStore.new(url)
17
+ when nil, "sqlite" then
18
+ SimpleCache::SqliteStore.new(uri.path)
19
+ else raise uri.scheme.inspect
20
+ end
21
+
22
+ cache.extend SimpleCache
23
+ end
24
+
25
+ singleton_class.class_eval do
26
+ attr :url, true
27
+
28
+ def store
29
+ Thread.current["simple_cache_store"] ||= new(url)
30
+ end
31
+ end
32
+
33
+ singleton_class.class_eval do
34
+ extend Forwardable
35
+ delegate [:fetch, :store, :cached, :clear] => :store
36
+ end
37
+
38
+ def cached(key, ttl = nil, &block)
39
+ fetch(key) do
40
+ store(key, yield, ttl)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,72 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "simple_cache"
8
+ s.version = "0.9.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["radiospiel"]
12
+ s.date = "2012-04-08"
13
+ s.description = "A sensibly fast, yet simplistic file based cache."
14
+ s.email = "eno@open-lab.org"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/simple_cache.rb",
28
+ "lib/simple_cache/marshal.rb",
29
+ "lib/simple_cache/redis_store.rb",
30
+ "lib/simple_cache/sqlite_store.rb",
31
+ "simple_cache.gemspec",
32
+ "test/redis_store_test.rb",
33
+ "test/sqlite_store_test.rb",
34
+ "test/test_helper.rb"
35
+ ]
36
+ s.homepage = "http://github.com/radiospiel/simple_cache"
37
+ s.licenses = ["MIT"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = "1.8.17"
40
+ s.summary = "You need a cache? Just throw me in."
41
+
42
+ if s.respond_to? :specification_version then
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ s.add_runtime_dependency(%q<sqlite3>, [">= 0"])
47
+ s.add_development_dependency(%q<bundler>, [">= 0"])
48
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
49
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
50
+ s.add_development_dependency(%q<ruby-debug19>, [">= 0"])
51
+ s.add_development_dependency(%q<timecop>, [">= 0"])
52
+ s.add_development_dependency(%q<redis>, [">= 0"])
53
+ else
54
+ s.add_dependency(%q<sqlite3>, [">= 0"])
55
+ s.add_dependency(%q<bundler>, [">= 0"])
56
+ s.add_dependency(%q<jeweler>, [">= 0"])
57
+ s.add_dependency(%q<simplecov>, [">= 0"])
58
+ s.add_dependency(%q<ruby-debug19>, [">= 0"])
59
+ s.add_dependency(%q<timecop>, [">= 0"])
60
+ s.add_dependency(%q<redis>, [">= 0"])
61
+ end
62
+ else
63
+ s.add_dependency(%q<sqlite3>, [">= 0"])
64
+ s.add_dependency(%q<bundler>, [">= 0"])
65
+ s.add_dependency(%q<jeweler>, [">= 0"])
66
+ s.add_dependency(%q<simplecov>, [">= 0"])
67
+ s.add_dependency(%q<ruby-debug19>, [">= 0"])
68
+ s.add_dependency(%q<timecop>, [">= 0"])
69
+ s.add_dependency(%q<redis>, [">= 0"])
70
+ end
71
+ end
72
+
@@ -0,0 +1,15 @@
1
+ require_relative 'test_helper'
2
+
3
+ class SimpleCache::RedisStoreTest < Test::Unit::TestCase
4
+ include SimpleCache::TestCase
5
+
6
+ URL = "redis://localhost/1"
7
+
8
+ def simple_cache
9
+ @simple_cache ||= SimpleCache.new(URL).tap(&:clear)
10
+ end
11
+
12
+ def teardown
13
+ @simple_cache.clear if @simple_cache
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'test_helper'
2
+
3
+ class SimpleCache::SqliteStoreTest < Test::Unit::TestCase
4
+ include SimpleCache::TestCase
5
+
6
+ def simple_cache
7
+ @simple_cache ||= SimpleCache.new("simple_cache_test").tap(&:clear)
8
+ end
9
+
10
+ def teardown
11
+ File.unlink @simple_cache.path if @simple_cache
12
+ end
13
+
14
+ def test_expiration
15
+ assert_equal("1", simple_cache.store("forever", "1"))
16
+ assert_equal("2", simple_cache.store("young", "2", 3))
17
+ Timecop.freeze(Time.now + 10) do
18
+ assert_equal("1", simple_cache.fetch("forever"))
19
+ assert_equal(nil, simple_cache.fetch("young"))
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,43 @@
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
+
11
+ require 'ruby-debug'
12
+ require 'simplecov'
13
+ require 'timecop'
14
+ require 'test/unit'
15
+ SimpleCov.start do
16
+ add_filter "test/*.rb"
17
+ end
18
+
19
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
20
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
21
+ require 'simple_cache'
22
+
23
+ module SimpleCache::TestCase
24
+ def test_simple_cache_kindof
25
+ assert simple_cache.kind_of?(SimpleCache)
26
+ end
27
+
28
+ def test_simple_cache
29
+ assert_equal(nil, simple_cache.fetch("bar"))
30
+
31
+ assert_equal("foo", simple_cache.store("bar", "foo"))
32
+ assert_equal("foo", simple_cache.fetch("bar"))
33
+
34
+ done = 0
35
+ assert_equal "baz", simple_cache.cached("key") { done += 1; "baz" }
36
+ assert_equal 1, done
37
+ assert_equal "baz", simple_cache.cached("key") { done += 1; "baz" }
38
+ assert_equal 1, done
39
+
40
+ assert_equal(nil, simple_cache.store("bar", nil))
41
+ assert_equal(nil, simple_cache.fetch("bar"))
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_cache_rs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - radiospiel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-09 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sqlite3
16
+ requirement: &70237177950000 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70237177950000
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &70237177949240 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70237177949240
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &70237177948560 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70237177948560
47
+ - !ruby/object:Gem::Dependency
48
+ name: simplecov
49
+ requirement: &70237177947820 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70237177947820
58
+ - !ruby/object:Gem::Dependency
59
+ name: ruby-debug19
60
+ requirement: &70237177947140 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70237177947140
69
+ - !ruby/object:Gem::Dependency
70
+ name: timecop
71
+ requirement: &70237177946480 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70237177946480
80
+ - !ruby/object:Gem::Dependency
81
+ name: redis
82
+ requirement: &70237177945820 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70237177945820
91
+ description: A sensibly fast, yet simplistic file based cache.
92
+ email: eno@open-lab.org
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files:
96
+ - LICENSE.txt
97
+ - README.rdoc
98
+ files:
99
+ - .document
100
+ - Gemfile
101
+ - Gemfile.lock
102
+ - LICENSE.txt
103
+ - README.rdoc
104
+ - Rakefile
105
+ - VERSION
106
+ - lib/simple_cache.rb
107
+ - lib/simple_cache/marshal.rb
108
+ - lib/simple_cache/redis_store.rb
109
+ - lib/simple_cache/sqlite_store.rb
110
+ - simple_cache.gemspec
111
+ - test/redis_store_test.rb
112
+ - test/sqlite_store_test.rb
113
+ - test/test_helper.rb
114
+ homepage: http://github.com/radiospiel/simple_cache
115
+ licenses:
116
+ - MIT
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ segments:
128
+ - 0
129
+ hash: -2088364848885140711
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 1.8.10
139
+ signing_key:
140
+ specification_version: 3
141
+ summary: You need a cache? Just throw me in.
142
+ test_files: []