db-cache-store 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1785318f4ba27a34f7b2c5b0492ad0c2beb59dd0
4
+ data.tar.gz: e58cb046187c93750b45f9dd2b5fbce754aabfcc
5
+ SHA512:
6
+ metadata.gz: c4a85a4891d329bf9d9fe8b0f159f1b84cf31205d087e343b51f42c1a1a36c33d5d452394b5e03c4797972dcf646a854198fd450b3fc35fcad3d296421a424f6
7
+ data.tar.gz: 40b21a8b46042b270d6bedfa49e15624419c8dea72d81cb3319c8f15e5081d2c2ef690b46a9cd443b716e1b66ca7ed281fc9e3cfae79f8ea69e92ce94c22c5d9
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ar_cache_store.gemspec
6
+ gemspec
@@ -0,0 +1,135 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ db-cache-store (0.1.0)
5
+ rails (>= 5.0, < 5.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (5.1.6)
11
+ actionpack (= 5.1.6)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (~> 0.6.1)
14
+ actionmailer (5.1.6)
15
+ actionpack (= 5.1.6)
16
+ actionview (= 5.1.6)
17
+ activejob (= 5.1.6)
18
+ mail (~> 2.5, >= 2.5.4)
19
+ rails-dom-testing (~> 2.0)
20
+ actionpack (5.1.6)
21
+ actionview (= 5.1.6)
22
+ activesupport (= 5.1.6)
23
+ rack (~> 2.0)
24
+ rack-test (>= 0.6.3)
25
+ rails-dom-testing (~> 2.0)
26
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
+ actionview (5.1.6)
28
+ activesupport (= 5.1.6)
29
+ builder (~> 3.1)
30
+ erubi (~> 1.4)
31
+ rails-dom-testing (~> 2.0)
32
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
33
+ activejob (5.1.6)
34
+ activesupport (= 5.1.6)
35
+ globalid (>= 0.3.6)
36
+ activemodel (5.1.6)
37
+ activesupport (= 5.1.6)
38
+ activerecord (5.1.6)
39
+ activemodel (= 5.1.6)
40
+ activesupport (= 5.1.6)
41
+ arel (~> 8.0)
42
+ activesupport (5.1.6)
43
+ concurrent-ruby (~> 1.0, >= 1.0.2)
44
+ i18n (>= 0.7, < 2)
45
+ minitest (~> 5.1)
46
+ tzinfo (~> 1.1)
47
+ arel (8.0.0)
48
+ builder (3.2.3)
49
+ coderay (1.1.2)
50
+ concurrent-ruby (1.0.5)
51
+ crass (1.0.4)
52
+ docile (1.3.0)
53
+ erubi (1.7.1)
54
+ globalid (0.4.1)
55
+ activesupport (>= 4.2.0)
56
+ i18n (1.0.0)
57
+ concurrent-ruby (~> 1.0)
58
+ json (2.1.0)
59
+ loofah (2.2.2)
60
+ crass (~> 1.0.2)
61
+ nokogiri (>= 1.5.9)
62
+ mail (2.7.0)
63
+ mini_mime (>= 0.1.1)
64
+ method_source (0.9.0)
65
+ mini_mime (1.0.0)
66
+ mini_portile2 (2.3.0)
67
+ minitest (5.11.3)
68
+ nio4r (2.3.0)
69
+ nokogiri (1.8.2)
70
+ mini_portile2 (~> 2.3.0)
71
+ pry (0.11.3)
72
+ coderay (~> 1.1.0)
73
+ method_source (~> 0.9.0)
74
+ rack (2.0.4)
75
+ rack-test (1.0.0)
76
+ rack (>= 1.0, < 3)
77
+ rails (5.1.6)
78
+ actioncable (= 5.1.6)
79
+ actionmailer (= 5.1.6)
80
+ actionpack (= 5.1.6)
81
+ actionview (= 5.1.6)
82
+ activejob (= 5.1.6)
83
+ activemodel (= 5.1.6)
84
+ activerecord (= 5.1.6)
85
+ activesupport (= 5.1.6)
86
+ bundler (>= 1.3.0)
87
+ railties (= 5.1.6)
88
+ sprockets-rails (>= 2.0.0)
89
+ rails-dom-testing (2.0.3)
90
+ activesupport (>= 4.2.0)
91
+ nokogiri (>= 1.6)
92
+ rails-html-sanitizer (1.0.4)
93
+ loofah (~> 2.2, >= 2.2.2)
94
+ railties (5.1.6)
95
+ actionpack (= 5.1.6)
96
+ activesupport (= 5.1.6)
97
+ method_source
98
+ rake (>= 0.8.7)
99
+ thor (>= 0.18.1, < 2.0)
100
+ rake (10.5.0)
101
+ simplecov (0.16.1)
102
+ docile (~> 1.1)
103
+ json (>= 1.8, < 3)
104
+ simplecov-html (~> 0.10.0)
105
+ simplecov-html (0.10.2)
106
+ sprockets (3.7.1)
107
+ concurrent-ruby (~> 1.0)
108
+ rack (> 1, < 3)
109
+ sprockets-rails (3.2.1)
110
+ actionpack (>= 4.0)
111
+ activesupport (>= 4.0)
112
+ sprockets (>= 3.0.0)
113
+ sqlite3 (1.3.13)
114
+ thor (0.20.0)
115
+ thread_safe (0.3.6)
116
+ tzinfo (1.2.5)
117
+ thread_safe (~> 0.1)
118
+ websocket-driver (0.6.5)
119
+ websocket-extensions (>= 0.1.0)
120
+ websocket-extensions (0.1.3)
121
+
122
+ PLATFORMS
123
+ ruby
124
+
125
+ DEPENDENCIES
126
+ bundler (~> 1.16)
127
+ db-cache-store!
128
+ minitest (>= 5.0.0)
129
+ pry
130
+ rake (~> 10.0)
131
+ simplecov
132
+ sqlite3
133
+
134
+ BUNDLED WITH
135
+ 1.16.0
@@ -0,0 +1,56 @@
1
+ # DbCacheStore
2
+
3
+ Alternative rails cache engine, that will use database with `activerecord`
4
+
5
+ ![What!?](https://media.giphy.com/media/3mJMxWRhmeiGukabki/giphy.gif)
6
+
7
+ ## Install
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'db-cache-store'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Global Usage
18
+
19
+ It's __NOT__ recommened to use it as global application cache because it's slow.
20
+
21
+ Supported rails versions: '>= 5.0', '< 5.2'
22
+
23
+ First you need to genereate table:
24
+
25
+ ```ruby
26
+ DbCacheStore:
27
+ db_cache_store:migration [TABLE_NAME=db_cache_store_items]
28
+ ```
29
+
30
+ Then declare your cache model and update configuration in application.rb:
31
+
32
+ ```ruby
33
+ class MyCacheRepository < ActiveSupport::Cache::DbItem
34
+ self.table_name = :cache_items
35
+ end
36
+ ```
37
+
38
+ ```ruby
39
+ config.cache_store = :db_cache_store, repository: MyCacheRepository
40
+ ```
41
+
42
+ ### Local usage
43
+
44
+ ```ruby
45
+ cache_store = ActiveSupport::Cache.lookup_store(:db_store, repository: MyCacheRepository)
46
+ ```
47
+
48
+ ## Development
49
+
50
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
+
52
+ `rake` will run unit tests
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/caulfield/db-cache-store
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.test_files = FileList['test/**/*_test.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task default: %i[test]
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "db_cache_store"
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(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "db_cache_store/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "db-cache-store"
7
+ spec.version = DbCacheStore::VERSION
8
+ spec.authors = ["Sergey Kuchmistov"]
9
+ spec.email = ["sergey.kuchmistov@gmail.com"]
10
+
11
+ spec.summary = "Alternative rails cache engine, that will use database server"
12
+ spec.description = "Alternative rails cache engine, that will use database server"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'rails', '>= 5.0', '< 5.2'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.16'
24
+ spec.add_development_dependency 'minitest', '>= 5.0.0'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'sqlite3'
27
+ spec.add_development_dependency 'simplecov'
28
+ spec.add_development_dependency 'pry'
29
+ end
@@ -0,0 +1,24 @@
1
+ require 'active_record'
2
+
3
+ module ActiveSupport
4
+ module Cache
5
+ class DbItem < ActiveRecord::Base
6
+ TABLE_NAME = 'db_cache_store_items'
7
+
8
+ self.table_name = TABLE_NAME
9
+
10
+ def entry
11
+ serialized_value = attributes['entry']
12
+ YAML.load(serialized_value) if serialized_value
13
+ end
14
+
15
+ def value
16
+ entry.value
17
+ end
18
+
19
+ def expired?
20
+ entry.expired?
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,44 @@
1
+ require 'active_support/cache'
2
+ require 'active_support/cache/db_item'
3
+
4
+ module ActiveSupport
5
+ module Cache
6
+ class DbStore < Store
7
+ attr_reader :repository
8
+
9
+ def initialize(options = {})
10
+ super(options)
11
+ @repository = options.fetch(:repository, DbItem)
12
+ end
13
+
14
+ def clear(options = {})
15
+ @repository.update_all(deleted_at: Time.now)
16
+ end
17
+
18
+ def cleanup(options = {})
19
+ @repository.where.not(expired_at: nil).update_all(deleted_at: Time.now)
20
+ end
21
+
22
+ private
23
+
24
+ def read_entry(key, options = {})
25
+ scope = @repository.where(expired_at: nil).where(deleted_at: nil).order(version: :desc)
26
+ if options[:version]
27
+ scope = scope.where(version: options[:version])
28
+ end
29
+ scope.find_by(key: key)
30
+ end
31
+
32
+ def write_entry(key, entry, options = nil)
33
+ @repository.create \
34
+ key: key,
35
+ entry: YAML.dump(entry),
36
+ version: (read_entry(key, options)&.version.to_f + 1)
37
+ end
38
+
39
+ def delete_entry(key, options = nil)
40
+ read_entry(key)&.touch(:expired_at)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,6 @@
1
+ require "db_cache_store/version"
2
+ require 'active_support/cache/db_store'
3
+
4
+ module DbCacheStore
5
+ end
6
+
@@ -0,0 +1,3 @@
1
+ module DbCacheStore
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ require 'rails/generators/base'
2
+
3
+ module DbCacheStore
4
+ module Generators
5
+ class MigrationGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ argument :table_name, type: :string, default: ActiveSupport::Cache::DbItem::TABLE_NAME
9
+
10
+ desc "Generates a migration for given TABLE to use it as cache storage."
11
+
12
+ source_root File.expand_path("../templates", __FILE__)
13
+
14
+ def generate_migration
15
+ migration_template "migration.rb", "db/migrate/db_cache_store_create_#{table_name}.rb"
16
+ end
17
+
18
+ def migration_version
19
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
20
+ end
21
+
22
+ def self.next_migration_number(dir)
23
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DbCacheStoreCreate<%= table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
4
+ def change
5
+ create_table :<%= table_name %>, id: false do |t|
6
+ t.string :key
7
+ t.text :entry
8
+ t.integer :version, default: 1
9
+
10
+ t.datetime :expired_at
11
+ t.datetime :deleted_at
12
+ end
13
+
14
+ add_index :<%= table_name %>, :expired_at
15
+ add_index :<%= table_name %>, :deleted_at
16
+ add_index :<%= table_name %>, :key
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: db-cache-store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Kuchmistov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.2'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '5.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.2'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.16'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.16'
47
+ - !ruby/object:Gem::Dependency
48
+ name: minitest
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 5.0.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 5.0.0
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: sqlite3
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: simplecov
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: pry
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ description: Alternative rails cache engine, that will use database server
118
+ email:
119
+ - sergey.kuchmistov@gmail.com
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - ".gitignore"
125
+ - Gemfile
126
+ - Gemfile.lock
127
+ - README.md
128
+ - Rakefile
129
+ - bin/console
130
+ - bin/setup
131
+ - db_cache_store.gemspec
132
+ - lib/active_support/cache/db_item.rb
133
+ - lib/active_support/cache/db_store.rb
134
+ - lib/db_cache_store.rb
135
+ - lib/db_cache_store/version.rb
136
+ - lib/generators/db_cache_store/migration_generator.rb
137
+ - lib/generators/db_cache_store/templates/migration.rb
138
+ homepage:
139
+ licenses: []
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.6.11
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: Alternative rails cache engine, that will use database server
161
+ test_files: []