persistent-cache-storage-sqlite 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.ruby-version-jruby +1 -0
- data/.ruby-version-ruby +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +46 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/persistent-cache/storage_sqlite.rb +99 -0
- data/lib/persistent-cache/version.rb +9 -0
- data/persistent-cache-storage-sqlite.gemspec +28 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 69bfd51c30b5d12fe63503a76e7f944aa43df6be
|
4
|
+
data.tar.gz: 05bd972a027eb0aeb6db6a5d8c3608d5d1563ff6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3c0c402494d4bf3d84c06e396ea238d3728f210df12ff90188b02d49cb75b13735de836cc4118e7934234b529bd5dd5bb0084a772b02d7582bc3576f9829a60e
|
7
|
+
data.tar.gz: e984e9874264abd53b97a0ca9fe8a029119fc09469d3b47188be70c95cbbbe063ee7d6fa2a493031bf85942028514a4389615e4f3272a3f1c267b6b7dbc07909
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
persistent-cache-storage-sqlite
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.3.0
|
data/.ruby-version-jruby
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
jruby-9.0.4.0
|
data/.ruby-version-ruby
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.0.0-p643
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Ernst Van Graan
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# Persistent::Cache::StorageSQLite
|
2
|
+
|
3
|
+
This gem provides a SQLite storage back-end to Persistent::Cache. Please see https://rubygems.org/gems/persistent-cache.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'persistent-cache-storage-sqlite'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install persistent-cache-storage-sqlite
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Updates to the cache are written to the sqlite3 storage, with SQL driver timeout set to 30 seconds.
|
24
|
+
|
25
|
+
Tell Persistent::Cache to use this provider so:
|
26
|
+
|
27
|
+
require 'persistent-cache/storage_sqlite'
|
28
|
+
require 'persistent-cache'
|
29
|
+
|
30
|
+
freshness = nil # forever or freshness in seconds
|
31
|
+
cache = Persistent::Cache.new('sqlite-cache-name', freshness, Persistent::Cache::STORAGE_SQLITE)
|
32
|
+
|
33
|
+
## Development
|
34
|
+
|
35
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
36
|
+
|
37
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/evangraan/persistent-cache-storage-sqlite. This gem was sponsored by Hetzner (Pty) Ltd - http://hetzner.co.za
|
42
|
+
|
43
|
+
## License
|
44
|
+
|
45
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
46
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "persistent/cache/storage/sqlite"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'persistent-cache/storage_api'
|
2
|
+
require 'persistent-cache/version'
|
3
|
+
require "sqlite3"
|
4
|
+
require "base64"
|
5
|
+
require "eh/eh"
|
6
|
+
|
7
|
+
module Persistent
|
8
|
+
class StorageSQLite < Persistent::Cache::StorageApi::API
|
9
|
+
DB_TABLE = "key_value" unless defined? DB_TABLE; DB_TABLE.freeze
|
10
|
+
DB_TIMEOUT = 30000 unless defined? DB_TIMEOUT; DB_TIMEOUT.freeze
|
11
|
+
|
12
|
+
attr_accessor :storage_details
|
13
|
+
attr_accessor :storage_handler
|
14
|
+
|
15
|
+
def initialize(storage_details)
|
16
|
+
raise ArgumentError.new("Storage details not provided") if storage_details.nil? or storage_details == ""
|
17
|
+
@storage_details = storage_details
|
18
|
+
@storage_handler = connect_to_database
|
19
|
+
@storage_handler.busy_timeout = 30000
|
20
|
+
end
|
21
|
+
|
22
|
+
def save_key_value_pair(key, value, timestamp = nil)
|
23
|
+
delete_entry(key)
|
24
|
+
time_entry = timestamp.nil? ? Time.now.to_s : timestamp.to_s
|
25
|
+
EH::retry!(:args => [serialize(key), serialize(value), time_entry]) do
|
26
|
+
@storage_handler.execute("INSERT INTO #{DB_TABLE} (key, value, timestamp) VALUES(?, ?, ?)",serialize(key), serialize(value), time_entry)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def lookup_key(key)
|
31
|
+
EH::retry!(:args => [serialize(key)]) do
|
32
|
+
result = @storage_handler.execute("SELECT value, timestamp FROM #{DB_TABLE} WHERE key=?", serialize(key))
|
33
|
+
!result.nil? && !result.empty? ? [deserialize(result[0][0]), result[0][1]] : nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def delete_entry(key)
|
38
|
+
EH::retry!(:args => [serialize(key)]) do
|
39
|
+
@storage_handler.execute("DELETE FROM #{DB_TABLE} WHERE key=?", serialize(key))
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def size
|
44
|
+
EH::retry!(:args => []) do
|
45
|
+
@storage_handler.execute("SELECT value FROM #{DB_TABLE}").size
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def keys
|
50
|
+
EH::retry!(:args => []) do
|
51
|
+
@storage_handler.execute("SELECT key FROM #{DB_TABLE}").collect { |k| deserialize(k[0]) }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def clear
|
56
|
+
EH::retry!(:args => []) do
|
57
|
+
@storage_handler.execute("DELETE FROM #{DB_TABLE}")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def serialize(data)
|
64
|
+
Base64.encode64(Marshal.dump(data))
|
65
|
+
end
|
66
|
+
|
67
|
+
def deserialize(data)
|
68
|
+
Marshal.load(Base64.decode64(data))
|
69
|
+
|
70
|
+
rescue TypeError => ex
|
71
|
+
Marshal.load(data)
|
72
|
+
end
|
73
|
+
|
74
|
+
def connect_to_database
|
75
|
+
File.exists?(@storage_details) ? open_database : create_database
|
76
|
+
end
|
77
|
+
|
78
|
+
def open_database
|
79
|
+
EH::retry!(:args => []) do
|
80
|
+
SQLite3::Database.open(@storage_details)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def create_database
|
85
|
+
EH::retry!(:args => []) do
|
86
|
+
@storage_handler = SQLite3::Database.new(@storage_details)
|
87
|
+
create_table
|
88
|
+
end
|
89
|
+
@storage_handler
|
90
|
+
end
|
91
|
+
|
92
|
+
def create_table
|
93
|
+
EH::retry!(:args => []) do
|
94
|
+
@storage_handler.execute("CREATE TABLE #{DB_TABLE}(key TEXT PRIMARY KEY, value TEXT, timestamp TEXT)")
|
95
|
+
end
|
96
|
+
@storage_handler
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'persistent-cache/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "persistent-cache-storage-sqlite"
|
8
|
+
spec.version = Persistent::Cache::Storage::Sqlite::VERSION
|
9
|
+
spec.authors = ["Ernst Van Graan"]
|
10
|
+
spec.email = ["ernst.van.graan@hetzner.co.za"]
|
11
|
+
|
12
|
+
spec.summary = %q{Provides a SQLite storage back-end to Persistent::Cache}
|
13
|
+
spec.description = %q{Provides a SQLite storage back-end to Persistent::Cache}
|
14
|
+
spec.homepage = "https://github.com/evangraan/persistent-cache-storage-sqlite"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_dependency 'sqlite3', '1.3.10'
|
26
|
+
spec.add_dependency "persistent-cache-storage-api"
|
27
|
+
spec.add_dependency 'eh'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: persistent-cache-storage-sqlite
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ernst Van Graan
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-12 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.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.3.10
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.3.10
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: persistent-cache-storage-api
|
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: eh
|
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
|
+
description: Provides a SQLite storage back-end to Persistent::Cache
|
98
|
+
email:
|
99
|
+
- ernst.van.graan@hetzner.co.za
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".ruby-gemset"
|
107
|
+
- ".ruby-version"
|
108
|
+
- ".ruby-version-jruby"
|
109
|
+
- ".ruby-version-ruby"
|
110
|
+
- ".travis.yml"
|
111
|
+
- Gemfile
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- bin/console
|
116
|
+
- bin/setup
|
117
|
+
- lib/persistent-cache/storage_sqlite.rb
|
118
|
+
- lib/persistent-cache/version.rb
|
119
|
+
- persistent-cache-storage-sqlite.gemspec
|
120
|
+
homepage: https://github.com/evangraan/persistent-cache-storage-sqlite
|
121
|
+
licenses:
|
122
|
+
- MIT
|
123
|
+
metadata: {}
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
requirements: []
|
139
|
+
rubyforge_project:
|
140
|
+
rubygems_version: 2.5.1
|
141
|
+
signing_key:
|
142
|
+
specification_version: 4
|
143
|
+
summary: Provides a SQLite storage back-end to Persistent::Cache
|
144
|
+
test_files: []
|