sqlite_cache 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5a284585e5ff271e5008b315d5d713065b59afd1
4
+ data.tar.gz: 4c210f3b9b71eafe9c523e50aca564390d719372
5
+ SHA512:
6
+ metadata.gz: b6a4776c380d48af5960262a4496e02d80f95c57d168614f5e05cc5e315d2d4ee1c7eab5b583d9934b86e3e5379ee12896b2e042cc2b18c0c46addf3982958d9
7
+ data.tar.gz: 8e6976cb479d4754279823c6342788a44614c119a713d7b47df8275e4d58d65f40c455e74f005139a52c475d7100abc2278fe89d98211ab044451243fa5d7649
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ branches:
6
+ only:
7
+ - master
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sqlite_cache.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Lars Brillert
2
+
3
+ MIT License
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.
@@ -0,0 +1,45 @@
1
+ # SqliteCache
2
+
3
+ SqliteCache allows to use a SQLite database as storage for your cache. It comes with an API compatible with ActiveSupport::Cache and can though easily used in any Rails Application.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sqlite_cache'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sqlite_cache
18
+
19
+ ## Usage
20
+
21
+ To use SqliteCache just head over to e.g. your `production.rb`file and add the following line
22
+
23
+ ```ruby
24
+ config.cache_store = SqliteCache::Store.new
25
+ ```
26
+
27
+ This will use an inmemory SQLite database. You can also specify a file location
28
+
29
+ ```ruby
30
+ config.cache_store = SqliteCache::Store.new("tmp/cache.db")
31
+ ```
32
+
33
+ For more information about Rails Caching please have a look at the [Rails Cache Guide](http://guides.rubyonrails.org/caching_with_rails.html)
34
+
35
+ ## Testing
36
+
37
+ rake
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,8 @@
1
+ require "sequel"
2
+ require "active_support"
3
+
4
+ require "sqlite_cache/version"
5
+ require "sqlite_cache/store"
6
+
7
+ module SqliteCache
8
+ end
@@ -0,0 +1,97 @@
1
+ module SqliteCache
2
+ class Store < ActiveSupport::Cache::Store
3
+ attr_accessor :logger
4
+
5
+ def initialize(path = "", options = nil)
6
+ @options = options ? options.dup : {}
7
+ @logger = @options[:logger]
8
+ @max_cleanup_time = @options.fetch(:max_prune_time, 2)
9
+
10
+ if path.present?
11
+ @db = Sequel.connect("sqlite://#{path}")
12
+ else
13
+ @db = Sequel.sqlite
14
+ end
15
+
16
+ @db.create_table(:cache) do
17
+ String :key
18
+ String :value
19
+ end unless @db.table_exists?(:cache)
20
+
21
+ @data = @db[:cache]
22
+ end
23
+
24
+ def clear(options = nil)
25
+ @data.delete
26
+ rescue Sequel::Error => e
27
+ logger.error("Sequel::Error (#{e}): #{e.message}") if logger
28
+ nil
29
+ end
30
+
31
+ def cleanup(max_time = nil)
32
+ instrument(:cleanup, size: @data.count) do
33
+ start_time = Time.now
34
+ @data.each do |row|
35
+ entry = read_entry(row[:key], options)
36
+ delete_entry(row[:key], options) if entry && entry.expired?
37
+ return if (max_time && Time.now - start_time > max_time)
38
+ end
39
+ end
40
+ end
41
+
42
+ protected
43
+
44
+ def count
45
+ @data.count
46
+ end
47
+
48
+ # Read an entry from the cache.
49
+ def read_entry(key, options) # :nodoc:
50
+ deserialize_entry(@data.where(key: key).get(:value))
51
+ rescue Sequel::Error => e
52
+ logger.error("Sequel::Error (#{e}): #{e.message}") if logger
53
+ nil
54
+ end
55
+
56
+ # Write an entry to the cache.
57
+ def write_entry(key, entry, options) # :nodoc:
58
+ cleanup(@max_cleanup_time)
59
+
60
+ method = exist?(key) ? :update : :insert
61
+
62
+ @data.send(method, {key: key, value: Marshal.dump(entry)})
63
+ true
64
+ rescue Sequel::Error => e
65
+ logger.error("Sequel::Error (#{e}): #{e.message}") if logger
66
+ false
67
+ end
68
+
69
+ # Delete an entry from the cache.
70
+ def delete_entry(key, options) # :nodoc:
71
+ @data.where(key: key).delete
72
+ rescue Sequel::Error => e
73
+ logger.error("Sequel::Error (#{e}): #{e.message}") if logger
74
+ false
75
+ end
76
+
77
+ private
78
+
79
+ def serialize_entry(value)
80
+ if value
81
+ entry = Marshal.dump(value) rescue value
82
+ entry.is_a?(Entry) ? entry : Entry.new(entry)
83
+ else
84
+ nil
85
+ end
86
+ end
87
+
88
+ def deserialize_entry(raw_value)
89
+ if raw_value
90
+ entry = Marshal.load(raw_value) rescue raw_value
91
+ entry.is_a?(ActiveSupport::Cache::Entry) ? entry : ActiveSupport::Cache::Entry.new(entry)
92
+ else
93
+ nil
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,3 @@
1
+ module SqliteCache
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rspec'
5
+ require 'sqlite_cache'
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ # Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
@@ -0,0 +1,174 @@
1
+ require "spec_helper"
2
+
3
+ class Pony
4
+
5
+ attr_accessor :name
6
+
7
+ def initialize(name)
8
+ self.name = name
9
+ end
10
+
11
+ def ==(other)
12
+ self.name == other.name
13
+ end
14
+
15
+ end
16
+
17
+ describe SqliteCache::Store do
18
+
19
+
20
+ describe "#fetch" do
21
+ describe "with cache hit" do
22
+ before { subject.write("foo", "bar") }
23
+
24
+ it "will return the cached value" do
25
+ expect(subject.fetch("foo") { "baz" }).to eq("bar")
26
+ end
27
+
28
+ it "will the block value when forced" do
29
+ expect(subject.fetch("foo", force: true) { "baz" }).to eq("baz")
30
+ end
31
+
32
+ describe "when nil is cached" do
33
+ before { subject.write("foo", nil) }
34
+
35
+ it "will stick to the cache " do
36
+ expect(subject.fetch("foo") { "baz" }).to be_nil
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ describe "with cache miss" do
43
+ before { subject.clear }
44
+
45
+ it "will return the value from the block" do
46
+ expect(subject.fetch("foo") { "baz" }).to eq("baz")
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ describe "#read" do
53
+
54
+ describe "value that wont expire" do
55
+ before { subject.write("foo", "bar") }
56
+
57
+ it "will return the value from the cache" do
58
+ expect(subject.read("foo")).to eq("bar")
59
+ end
60
+ end
61
+
62
+ describe "when a value expires" do
63
+ let(:time) { Time.local(2008, 4, 24) }
64
+ before do
65
+ Time.stub(now: time)
66
+ subject.write("foo", "bar", expires_in: 60.second)
67
+ end
68
+
69
+ it "will be removed from the cache" do
70
+ expect{ Time.stub(now: time + 61) }.to change{subject.read("foo")}.from("bar").to(nil)
71
+ end
72
+ end
73
+
74
+ describe "value is a complex type" do
75
+ let(:pony) { Pony.new("name") }
76
+
77
+ before { subject.write("foo", pony) }
78
+ it "will return the value from the cache" do
79
+ expect(subject.read("foo")).to eq(pony)
80
+ end
81
+ end
82
+ end
83
+
84
+ describe "#write" do
85
+
86
+ it "will persist a value into the cache" do
87
+ expect{subject.write("foo", "bar")}.not_to raise_error
88
+ expect(subject.read("foo")).to eq("bar")
89
+ end
90
+
91
+ describe "updating a existing key" do
92
+ before { subject.write("foo", "bar") }
93
+
94
+ it "will update a value in the cache" do
95
+ expect{subject.write("foo", "baz")}.to change{ subject.read("foo")}.from("bar").to("baz")
96
+ end
97
+ end
98
+
99
+ end
100
+
101
+ describe "#delete" do
102
+
103
+ before { subject.write("foo", "bar") }
104
+ it "will delte a key from the cache" do
105
+ expect{subject.delete("foo")}.to change{ subject.read("foo")}.from("bar").to(nil)
106
+ end
107
+ end
108
+
109
+ describe "#exist?" do
110
+ it "will delte a key from the cache" do
111
+ expect{subject.write("foo", "bar")}.to change{ subject.exist?("foo")}.from(nil).to(true)
112
+ end
113
+ end
114
+
115
+ describe "#clear" do
116
+ before do
117
+ subject.write("foo", "bar")
118
+ subject.write("john", "doe")
119
+ end
120
+
121
+ it "will delte all keys from the cache" do
122
+ subject.clear
123
+
124
+ expect(subject.exist?("foo")).to be_nil
125
+ expect(subject.exist?("john")).to be_nil
126
+ end
127
+ end
128
+
129
+ describe "#cleanup" do
130
+ let(:time) { Time.local(2008, 4, 24) }
131
+
132
+ before do
133
+ subject.clear
134
+
135
+ Time.stub(now: time)
136
+
137
+ subject.write("john", "doe", expires_in: 120.second)
138
+ subject.write("foo", "bar", expires_in: 60.second)
139
+
140
+ Time.stub(now: time + 61)
141
+ end
142
+
143
+ it "will delte all expired keys from the cache" do
144
+ expect{ subject.cleanup }.to change{ subject.send(:read_entry, "foo", {})}.to(nil)
145
+ end
146
+
147
+ it "will leave the others untouched" do
148
+ expect{ subject.cleanup }.not_to change{ subject.send(:read_entry, "john", {})}.to(nil)
149
+ end
150
+
151
+ describe "with max_time applied" do
152
+ before do
153
+ subject.clear
154
+
155
+ Time.stub(now: time)
156
+
157
+ subject.write("john", "doe", expires_in: 30.second)
158
+ subject.write("foo", "bar", expires_in: 30.second)
159
+
160
+ Time.stub(now: time + 60)
161
+ end
162
+
163
+ it "cannot cleanup all items when running out of time" do
164
+ expect{ subject.cleanup(-12) }.to change{ subject.send(:count)}.by(-1)
165
+ end
166
+
167
+ it "can cleanup all items when time is plenty" do
168
+ expect{ subject.cleanup(2) }.to change{ subject.send(:count)}.by(-2)
169
+ end
170
+ end
171
+
172
+ end
173
+
174
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sqlite_cache/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sqlite_cache"
8
+ spec.version = SqliteCache::VERSION
9
+ spec.authors = ["Lars Brillert"]
10
+ spec.email = ["lars@railslove.com"]
11
+ spec.description = %q{Use sqlite3 as a caching adapter}
12
+ spec.summary = %q{Use sqlite3 as a caching adapter}
13
+ spec.homepage = "http://railslove.com"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+
25
+ spec.add_dependency "sequel"
26
+ spec.add_dependency "activesupport"
27
+ spec.add_dependency "sqlite3"
28
+ spec.add_dependency "dalli"
29
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sqlite_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Lars Brillert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sequel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: dalli
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Use sqlite3 as a caching adapter
112
+ email:
113
+ - lars@railslove.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - .rspec
120
+ - .travis.yml
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - lib/sqlite_cache.rb
126
+ - lib/sqlite_cache/store.rb
127
+ - lib/sqlite_cache/version.rb
128
+ - spec/spec_helper.rb
129
+ - spec/sqlite_cache_store_spec.rb
130
+ - sqlite_cache.gemspec
131
+ homepage: http://railslove.com
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.0.6
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Use sqlite3 as a caching adapter
155
+ test_files:
156
+ - spec/spec_helper.rb
157
+ - spec/sqlite_cache_store_spec.rb