cache_lookup 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .rvmrc
6
+ coverage
7
+ InstalledFiles
8
+ lib/bundler/man
9
+ pkg
10
+ rdoc
11
+ spec/reports
12
+ test/tmp
13
+ test/version_tmp
14
+ tmp
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
20
+
21
+ *~
22
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cache_lookup.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tom Fakes
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.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # CacheLookup
2
+
3
+ Provide an alternative to **ActiveRecord#find** that uses the Rails cache to store retrieved items to reduce database load and increase
4
+ performance for subsequent lookups for the same item. **CacheLookup** also provides cache expiry for items when they are updated
5
+ or deleted from teh database.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'cache_lookup'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install cache_lookup
20
+
21
+ ## Quick Start
22
+
23
+ In your ActiveRecord model:
24
+
25
+ ```ruby
26
+ class Article < ActiveRecord::Base
27
+
28
+ cache_lookup :id
29
+ cache_lookup :slug
30
+
31
+ end
32
+ ```
33
+
34
+ In your controller:
35
+
36
+ ```ruby
37
+ class ArticleController < ApplicationController
38
+
39
+ def show
40
+ @article = Article.lookup_by_id(params[:id])
41
+ end
42
+ end
43
+ ```
44
+
45
+ ## Usage
46
+
47
+ #### cache_lookup(attribute)
48
+
49
+ Defines the attribute to use as the cache key, and for reading from the database. The attribute must return a single item when queried, otherwise
50
+ cache expiry will be broken.
51
+
52
+ Typically, the attribute will be 'id', but can be any attribute on the model.
53
+
54
+ ```ruby
55
+ class Article < ActiveRecord::Base
56
+
57
+ cache_lookup :id
58
+
59
+ end
60
+ ```
61
+
62
+ #### Model#lookup_by_<attribute>
63
+
64
+ The method **lookup_by_<attribute>** will be added to your model. In the above example, this will be **lookup_by_id**. When used with the 'id' attribute,
65
+ this can be a direct replacement for Model.find(id).
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create new Pull Request
74
+
75
+ ## Credits
76
+
77
+ [<img src="http://d1za39ny3bo0r4.cloudfront.net/assets/craz8-logo-8e807e1c44376d564da419a6d82ec5be.png">](http://craz8.com)
78
+
79
+ CacheLookup is maintained by CRAZ8
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/cache_lookup/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Tom Fakes"]
6
+ gem.email = ["craz8@tomandlisa.us"]
7
+ gem.description = "Provide a cache system for single ActiveRecord objects with expiry on update and delete"
8
+ gem.summary = "Cached lookup of ActiveRecord objects with expiry on update"
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "cache_lookup"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = CacheLookup::VERSION
17
+
18
+ gem.add_dependency "activesupport", ">= 3.0"
19
+ gem.add_dependency "rails", ">= 3.0"
20
+ end
@@ -0,0 +1,36 @@
1
+ require "cache_lookup/version"
2
+ require 'active_support/core_ext'
3
+ require 'active_support/cache'
4
+
5
+ require 'cache_lookup/railtie.rb' if defined?(Rails)
6
+
7
+ module CacheLookup
8
+ extend ActiveSupport::Concern
9
+
10
+ module ClassMethods
11
+ def cache_lookup(attribute)
12
+
13
+ # post.clear_id_cache
14
+ define_method("clear_#{attribute}_cache") do
15
+ Rails.cache.delete self.class.send("cache_key_for_#{attribute}", send("#{attribute}"))
16
+ end
17
+
18
+ # ActiveRecord callbacks on Save and Destroy to clear the cache
19
+ after_commit "clear_#{attribute}_cache".to_sym
20
+ after_destroy "clear_#{attribute}_cache".to_sym
21
+
22
+ # Post.lookup_by_id
23
+ define_singleton_method("lookup_by_#{attribute}") do |param|
24
+ Rails.cache.fetch(send("cache_key_for_#{attribute}", param)) do
25
+ where(attribute => param).first
26
+ end
27
+ end
28
+
29
+ # Post.cache_key_for_id
30
+ define_singleton_method("cache_key_for_#{attribute}") do |param|
31
+ "#{name}:#{attribute}:" + param.to_s
32
+ end
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,11 @@
1
+ module CacheLookup
2
+ class Railtie < ::Rails::Railtie
3
+
4
+ initializer 'cache_lookup.initialize' do
5
+ ActiveSupport.on_load(:active_record) do
6
+ ActiveRecord::Base.send :include, CacheLookup
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module CacheLookup
2
+ VERSION = "0.0.2"
3
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cache_lookup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tom Fakes
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '3.0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '3.0'
46
+ description: Provide a cache system for single ActiveRecord objects with expiry on
47
+ update and delete
48
+ email:
49
+ - craz8@tomandlisa.us
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - cache_lookup.gemspec
60
+ - lib/cache_lookup.rb
61
+ - lib/cache_lookup/railtie.rb
62
+ - lib/cache_lookup/version.rb
63
+ homepage: ''
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.24
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Cached lookup of ActiveRecord objects with expiry on update
87
+ test_files: []