activerecord_cache_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.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in activerecord_cache_store.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,23 @@
1
+ Installation
2
+ ============
3
+
4
+ gem install activerecord_cache_store
5
+
6
+ Setup
7
+ =====
8
+
9
+ in your environment:
10
+
11
+ config.cache_store = :active_record_store
12
+
13
+
14
+ Cache Migration
15
+ ===============
16
+
17
+ rails g cache_migration
18
+
19
+
20
+ Notes
21
+ =====
22
+
23
+ https://rails.lighthouseapp.com/projects/8994/tickets/4452-making-activesupportcache-consistent
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "activerecord_cache_store"
6
+ s.version = "0.0.1"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Tomasz Mazur"]
9
+ s.email = ["defkode@gmail.com"]
10
+ s.homepage = "http://rubygems.org/gems/activerecord_cache_store"
11
+ s.summary = %q{ActiveRecord Cache Store for Rails 3}
12
+ s.description = %q{stores cache in database}
13
+
14
+ s.rubyforge_project = "activerecord_cache_store"
15
+ s.add_dependency("activerecord", ">= 3.0.0")
16
+ s.add_dependency("activesupport", ">= 3.0.0")
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,8 @@
1
+ require "generators/cache_generator"
2
+
3
+ module ActiveSupport
4
+ module Cache
5
+ autoload :ActiveRecordStore, 'active_support/cache/active_record_store'
6
+ end
7
+ end
8
+
@@ -0,0 +1,17 @@
1
+ class CacheEntry < ActiveRecord::Base
2
+ set_table_name :cache
3
+
4
+ validates :key, :presence => true, :uniqueness => true
5
+ validates :value, :presence => true
6
+ validates :created_at, :presence => true
7
+
8
+ scope :expired, lambda { where("expires_at < ?", Time.now.to_f) }
9
+
10
+ def expires_in
11
+ expires_at ? expires_at - created_at : nil
12
+ end
13
+
14
+ def to_cache_entry
15
+ ActiveSupport::Cache::Entry.create(value, created_at, :expires_in => expires_in)
16
+ end
17
+ end
@@ -0,0 +1,41 @@
1
+ require "active_record/cache_entry"
2
+
3
+ module ActiveSupport
4
+ module Cache
5
+ class ActiveRecordStore < Store
6
+
7
+ def cleanup(options = nil)
8
+ ::CacheEntry.expired.delete_all
9
+ end
10
+
11
+ def clear(options = nil)
12
+ ::CacheEntry.delete_all
13
+ end
14
+
15
+ private
16
+
17
+ def read_entry(key, options)
18
+ if entry = ::CacheEntry.find_by_key(key)
19
+ entry.to_cache_entry
20
+ end
21
+ end
22
+
23
+ def write_entry(key, entry, options)
24
+ if cache = ::CacheEntry.find_by_key(key)
25
+ delete_entry(key, {})
26
+ end
27
+ ::CacheEntry.create!({
28
+ :key => key,
29
+ :value => entry.value,
30
+ :created_at => entry.created_at,
31
+ :expires_at => entry.expires_at
32
+ })
33
+ end
34
+
35
+ def delete_entry(key, options)
36
+ puts "DELETE"
37
+ ::CacheEntry.where(:key => key).delete_all
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,14 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module Rails
4
+ module Generators
5
+ class CacheMigrationGenerator < ActiveRecord::Generators::Base
6
+ argument :name, :type => :string, :default => ""
7
+ source_root File.expand_path("../templates", __FILE__)
8
+
9
+ def create_migration_file
10
+ migration_template "migration.rb", "db/migrate/create_cache.rb"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ class CreateCache < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :cache do |t|
4
+ t.string :key
5
+ t.string :value
6
+ t.float :expires_in
7
+ t.float :created_at
8
+ t.float :expires_at
9
+ end
10
+
11
+ add_index :cache, :key, :unique => true
12
+ add_index :cache, :created_at
13
+ add_index :cache, :expires_at
14
+ end
15
+
16
+ def self.down
17
+ drop_table :cache
18
+
19
+ remove_index :cache, :key
20
+ remove_index :cache, :created_at
21
+ remove_index :cache, :expires_at
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_cache_store
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Tomasz Mazur
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-07 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activerecord
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: activesupport
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 7
46
+ segments:
47
+ - 3
48
+ - 0
49
+ - 0
50
+ version: 3.0.0
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ description: stores cache in database
54
+ email:
55
+ - defkode@gmail.com
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ extra_rdoc_files: []
61
+
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - README
66
+ - Rakefile
67
+ - activerecord_cache_store.gemspec
68
+ - activerecord_cache_store.rb
69
+ - lib/active_record/cache_entry.rb
70
+ - lib/active_support/cache/active_record_store.rb
71
+ - lib/generators/cache_migration_generator.rb
72
+ - lib/generators/templates/migration.rb
73
+ has_rdoc: true
74
+ homepage: http://rubygems.org/gems/activerecord_cache_store
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options: []
79
+
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ requirements: []
101
+
102
+ rubyforge_project: activerecord_cache_store
103
+ rubygems_version: 1.3.7
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: ActiveRecord Cache Store for Rails 3
107
+ test_files: []
108
+