activesupport-database_store 0.0.1

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
+ SHA256:
3
+ metadata.gz: b2c4cef104bdaa69993b4953262f83b5a95468f96cd0819f77567c27d5d72f70
4
+ data.tar.gz: 4215452f1294d675693a4dda8f8d422f07fabffd8d5de9b94afd3acec25ba675
5
+ SHA512:
6
+ metadata.gz: 894f68ee49f84b8192d4fb8de839989f40eb4293496bc288734482f482e525db030e79d8d7dfe1f83d9e330bb98d7f9d99191e6f285da1d101d0ef7533bda598
7
+ data.tar.gz: 6c60d7f42d8a3f332b314ab62926060fa298a2a14b5fcd7f60bc643f61909aa3b0723bcc06bce7b747be28161f2dd521b36fb209b9c1f53d75df3d2b401bdc46
@@ -0,0 +1,31 @@
1
+ # ActiveSupport::Cache::DatabaseStore
2
+ for Rails5
3
+
4
+
5
+ ## Usage
6
+ Add to your Gemfile
7
+ ```
8
+ gem 'activesupport-database_store', github: 'yiiz/activesupport-database_store'
9
+ ```
10
+
11
+ Create `stores` table by `rails g migration create_stores`
12
+ ```
13
+ def change
14
+ create_table :stores, id: false do |t|
15
+ t.string :key, null: false
16
+ t.binary :entry
17
+ end
18
+ add_index :stores, :key, unique: true
19
+ end
20
+ ```
21
+
22
+ Config `config/application.rb`
23
+ ```
24
+ config.cache_store = :database_store
25
+ ```
26
+
27
+
28
+ ## TODO
29
+ - generator migration task
30
+ - implement all store methods
31
+ - custom table name
@@ -0,0 +1,44 @@
1
+ module ActiveSupport
2
+ module Cache
3
+ class DatabaseStore < Store
4
+ class Store < ActiveRecord::Base
5
+ self.primary_key = 'key'
6
+ end
7
+
8
+ def clear(options = nil)
9
+ Store.delete_all
10
+ end
11
+
12
+ # def cleanup(options = nil)
13
+ # end
14
+
15
+ # def increment(name, amount = 1, options = nil)
16
+ # end
17
+
18
+ # def decrement(name, amount = 1, options = nil)
19
+ # end
20
+
21
+ # def delete_matched(matcher, options = nil)
22
+ # end
23
+
24
+ private
25
+ def read_entry(key, options)
26
+ entry = Store.where(key: key).limit(1).pluck(:entry).first
27
+ return if entry.nil?
28
+ Marshal.load(entry)
29
+ end
30
+
31
+ def write_entry(key, entry, options)
32
+ return false if options[:unless_exist] && Store.exist?(key)
33
+ store = Store.find_or_initialize_by(key: key)
34
+ store.entry = Marshal.dump(entry)
35
+ store.save
36
+ end
37
+
38
+ def delete_entry(key, options)
39
+ Store.where(key: key).delete_all
40
+ true
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,28 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module ActiveRecord
4
+ module Generators
5
+ class SessionMigrationGenerator < Base
6
+ source_root File.expand_path("../templates", __FILE__)
7
+ argument :name, :type => :string, :default => "add_sessions_table"
8
+
9
+ def create_migration_file
10
+ migration_template "migration.rb", "db/migrate/#{file_name}.rb"
11
+ end
12
+
13
+ protected
14
+
15
+ def session_table_name
16
+ current_table_name = ActiveRecord::SessionStore::Session.table_name
17
+ if current_table_name == 'session' || current_table_name == 'sessions'
18
+ current_table_name = ActiveRecord::Base.pluralize_table_names ? 'sessions' : 'session'
19
+ end
20
+ current_table_name
21
+ end
22
+
23
+ def migration_version
24
+ "[#{ActiveRecord::Migration.current_version}]" if ActiveRecord::Migration.respond_to?(:current_version)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,12 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :<%= session_table_name %> do |t|
4
+ t.string :session_id, :null => false
5
+ t.text :data
6
+ t.timestamps
7
+ end
8
+
9
+ add_index :<%= session_table_name %>, :session_id, :unique => true
10
+ add_index :<%= session_table_name %>, :updated_at
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ namespace 'db:sessions' do
2
+ desc "Creates a sessions migration for use with ActiveRecord::SessionStore"
3
+ task :create => [:environment, 'db:load_config'] do
4
+ Rails.application.load_generators
5
+ require 'generators/active_record/session_migration_generator'
6
+ ActiveRecord::Generators::SessionMigrationGenerator.start [ ENV['MIGRATION'] || 'add_sessions_table' ]
7
+ end
8
+
9
+ desc "Clear the sessions table"
10
+ task :clear => [:environment, 'db:load_config'] do
11
+ ActiveRecord::Base.connection.execute "TRUNCATE TABLE #{ActiveRecord::SessionStore::Session.table_name}"
12
+ end
13
+
14
+ desc "Trim old sessions from the table (default: > 30 days)"
15
+ task :trim => [:environment, 'db:load_config'] do
16
+ cutoff_period = (ENV['SESSION_DAYS_TRIM_THRESHOLD'] || 30).to_i.days.ago
17
+ ActiveRecord::SessionStore::Session.
18
+ where("updated_at < ?", cutoff_period).
19
+ delete_all
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activesupport-database_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ''
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ description:
42
+ email:
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - README.md
48
+ - lib/active_support/cache/database_store.rb
49
+ - lib/generators/active_record/session_migration_generator.rb
50
+ - lib/generators/active_record/templates/migration.rb
51
+ - lib/tasks/database.rake
52
+ homepage:
53
+ licenses: []
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 2.7.6
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: ''
75
+ test_files: []