custom_fragment_cache 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NWQyOTVjNmI4NDMyZTRlZjlhNTNmZWVjZWJmMTk1MTdmYjljYjhjMw==
5
+ data.tar.gz: !binary |-
6
+ YTVhOGViMDhlN2QxN2RmODlmNGNkMmMwMWFiMWVmZWEyZjVjZmViOA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NDlkOGMyNTMyYzhmOGRmYWM2YTQ3ZThhZDhhMGUwMDdlMGQ5ZDZmNWI0OWU0
10
+ ZTRjMTQxOWZmMjQ5ZDU3YjMwNjg1NGQwNjA4OWNmODdjMDgzY2JkOGZiZGFi
11
+ YmIzODZiYjQ0N2I2ZWZjOTlkM2U1YjU2MmQ5ZjUwMmRkNmFkZGE=
12
+ data.tar.gz: !binary |-
13
+ MWMxMWViYWUwMmRmN2Q2MGFiYzVlOTdlMDZhNGVhYjY5N2NlZWVmMWZhYmIw
14
+ NjI5YWMxODc5ZTAzMTg4OGRlNDBjMjk1MWE0ZGJjMDYzMzUyODc4MTI5MTcx
15
+ OGMxODA1NDE0MGYzZWEzMzk4YWZjYjFmNjBmMDMxNWRjNmZjYzc=
Binary file
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in custom_fragment_cache.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Yarin Goldman
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.
@@ -0,0 +1,66 @@
1
+ # CustomFragmentCache
2
+
3
+ CustomFragmentCache gives you a simple way for defining your own fragments cache with the ability to specify expiration according to fields.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'custom_fragment_cache'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install custom_fragment_cache
18
+
19
+ ## Usage
20
+ ### Define fragments:
21
+ Define it on ActiveRecord:
22
+ ``` ruby
23
+ class User < ActiveRecord::Base
24
+ include CustomFragmentCache
25
+
26
+ define_cache_fragment(:full_name, trigger_fields: %w(first_name last_name)) # When one of the trigger fields will be changed it will expire the cache
27
+ define_cache_fragment(:full_name, safe_fields: %w(id created_at updated_at)) # When one of the none safe fields will be changed it will expire the cache
28
+ end
29
+ ```
30
+
31
+ You can also define it on ActiveRecord::Observer
32
+ ``` ruby
33
+ class UserCache < ActiveRecord::Observer
34
+ include CustomFragmentCache
35
+ observe :user
36
+
37
+ define_cache_fragment(:full_name, trigger_fields: %w(first_name last_name))
38
+ end
39
+ ```
40
+
41
+ ### Use cache on the view:
42
+ Use `custom_cache` instead of `cache`
43
+ ``` ruby
44
+ <p>
45
+ <span>Hello </span>
46
+ <span><% custom_cache(User.first, :full_name) { "#{User.first.first_name} #{User.first.last_name}" } %></span>
47
+ </p>
48
+ ```
49
+
50
+ Jbuilder support
51
+ ``` ruby
52
+ json.custom_cache!(user, :full_name) do
53
+ json.first_name user.first_name
54
+ json.last_name user.last_name
55
+ end
56
+ ```
57
+
58
+
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it ( http://github.com/<my-github-username>/custom_fragment_cache/fork )
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'custom_fragment_cache/version'
5
+ require 'active_support/concern'
6
+ require 'active_support/cache'
7
+ require 'custom_fragment_cache/logic'
8
+ require 'custom_fragment_cache/fragment'
9
+ require 'custom_fragment_cache/configuration'
10
+
11
+ Gem::Specification.new do |spec|
12
+ spec.name = "custom_fragment_cache"
13
+ spec.version = CustomFragmentCache::VERSION
14
+ spec.authors = ["Yarin Goldman"]
15
+ spec.email = ["yarin@gettaxi.com"]
16
+ spec.summary = %q{Define your custom fragment caches}
17
+ spec.description = %q{Define your custom fragment caches}
18
+ spec.homepage = ""
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files -z`.split("\x0")
22
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
23
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_runtime_dependency 'activesupport', '>= 3.0'
27
+ spec.add_development_dependency "bundler", "~> 1.5"
28
+ spec.add_development_dependency "rake"
29
+ spec.add_development_dependency "rspec"
30
+ spec.add_development_dependency "pry"
31
+ end
@@ -0,0 +1,33 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/core_ext/object/blank'
3
+ require 'custom_fragment_cache/railtie' if defined?(Rails)
4
+ require 'custom_fragment_cache/logic'
5
+ require 'custom_fragment_cache/fragment'
6
+ require 'custom_fragment_cache/helper'
7
+ require 'custom_fragment_cache/cache_helper_method'
8
+ require 'custom_fragment_cache/configuration'
9
+ require 'custom_fragment_cache/version'
10
+
11
+ module CustomFragmentCache
12
+ extend ActiveSupport::Concern
13
+ include Logic
14
+
15
+ included do
16
+ # Don't call after_save when it's Observer
17
+ after_save :fragments_validity unless self.superclass == ActiveRecord::Observer
18
+ end
19
+
20
+ # Observer support
21
+ def after_save(record)
22
+ fragments_validity(record)
23
+ end
24
+
25
+ def fragments_validity(record = self)
26
+ self.class.fragment_caches.each do |fragment_cache|
27
+ if expire_cache?(fragment_cache, record)
28
+ expire_fragment(cache_key(fragment_cache.name, record))
29
+ end
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,13 @@
1
+ module CustomFragmentCache
2
+ class CacheHelperMethod
3
+ def self.base_custom_cache(context, method_name, resource, name, options = {}, &block)
4
+ if CustomFragmentCache.configuration.enabled
5
+ fragment_cache_key = ::CustomFragmentCache::Logic.cache_key(name, resource)
6
+ default_options = { expires_in: CustomFragmentCache.configuration.expiration_time }
7
+ context.__send__(method_name, fragment_cache_key, default_options.merge(options), &block)
8
+ else
9
+ block.call
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ module CustomFragmentCache
2
+ class Configuration
3
+ attr_accessor :enabled, :expiration_time
4
+
5
+ def initialize
6
+ @enabled = true
7
+ @expiration_time = 1.hour
8
+ end
9
+ end
10
+
11
+ def self.configuration
12
+ @configuration ||= Configuration.new
13
+ end
14
+
15
+ def self.configuration=(config)
16
+ @configuration = config
17
+ end
18
+
19
+ def self.configure
20
+ yield(configuration) if block_given?
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ module CustomFragmentCache
2
+ class Fragment
3
+ attr_reader :name, :opts, :fields, :fields_method
4
+
5
+ def initialize(name, opts)
6
+ @name = name
7
+ @opts = opts.with_indifferent_access
8
+ @fields = {}
9
+ @fields_method = :none
10
+ set_fields
11
+ end
12
+
13
+ def set_fields
14
+ validate_fields_method
15
+
16
+ if @opts[:safe_fields].present?
17
+ @fields = @opts[:safe_fields].to_a
18
+ @fields_method = :safe
19
+ elsif @opts[:trigger_fields].present?
20
+ @fields = @opts[:trigger_fields].to_a
21
+ @fields_method = :trigger
22
+ end
23
+ end
24
+
25
+ def validate_fields_method
26
+ if @opts.key?(:safe_fields) && @opts.key?(:trigger_fields)
27
+ raise ArgumentError, "Set trigger and safe fields are not allowed together"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ module CustomFragmentCache
2
+ module Helper
3
+ def custom_cache(resource, name, options = {}, &block)
4
+ CacheHelperMethod.base_custom_cache(self, :cache, resource, name, options, &block)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,56 @@
1
+ module CustomFragmentCache
2
+ module Logic
3
+ extend ActiveSupport::Concern
4
+
5
+ def expire_cache?(fragment_cache, record)
6
+ if fragment_cache.fields_method == :safe
7
+ unsafe_dirty_fields = record.changed - fragment_cache.fields
8
+ return true if unsafe_dirty_fields.present?
9
+ elsif fragment_cache.fields_method == :trigger
10
+ trigger_dirty_fields = record.changed & fragment_cache.fields
11
+ return true if trigger_dirty_fields.present?
12
+ end
13
+ end
14
+
15
+ def expire_fragment(key)
16
+ action_controller = ActionController::Base.new
17
+
18
+ if action_controller.send(:cache_configured?)
19
+ action_controller.instrument_fragment_cache :expire_fragment, key do
20
+ if key.is_a?(Regexp)
21
+ action_controller.cache_store.delete_matched(key)
22
+ elsif action_controller.cache_store.is_a?(ActiveSupport::Cache::RedisStore)
23
+ # TODO find better solution
24
+ action_controller.cache_store.delete_matched("*#{key}*")
25
+ else
26
+ action_controller.cache_store.delete(key)
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ def prefix
33
+ "custom_fragment_cache"
34
+ end
35
+
36
+ def cache_key(fragment_name, record)
37
+ "#{prefix}:#{record.class.name.downcase}-#{record.id}:#{fragment_name}"
38
+ end
39
+ module_function :cache_key, :prefix
40
+
41
+ module ClassMethods
42
+ def add_fragment_cache(fragment_cache)
43
+ (@fragment_caches ||= []) << fragment_cache
44
+ end
45
+
46
+ def fragment_caches
47
+ @fragment_caches || []
48
+ end
49
+
50
+ def define_cache_fragment(name, opts = {})
51
+ fragment_cache = CustomFragmentCache::Fragment.new(name, opts)
52
+ add_fragment_cache(fragment_cache)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,15 @@
1
+ module CustomFragmentCache
2
+ class Railtie < Rails::Railtie
3
+ initializer "custom_fragment_cache.extend_cache_helpers" do |app|
4
+ ActiveSupport.on_load :action_view do
5
+ include Helper
6
+ end
7
+
8
+ JbuilderTemplate.class_eval do
9
+ def custom_cache!(resource, name, options = {}, &block)
10
+ CacheHelperMethod.base_custom_cache(self, :cache!, resource, name, options, &block)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module CustomFragmentCache
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+ require 'pry'
3
+ require 'custom_fragment_cache'
4
+
5
+ describe CustomFragmentCache do
6
+ class DummyClass
7
+ include CustomFragmentCache::Logic
8
+ end
9
+
10
+ describe "#expire_cache?" do
11
+ context "safe fields" do
12
+ it "returns true when some non safe field changed" do
13
+ cache = Object.new
14
+ cache.stub(:fields_method) { :safe }
15
+ cache.stub(:fields) { ["a"] }
16
+ resource = Object.new
17
+ resource.stub(:changed) { ["a", "b"] }
18
+ DummyClass.new.expire_cache?(cache, resource).should be_true
19
+ end
20
+
21
+ it "returns false when only safe fields changed" do
22
+ cache = Object.new
23
+ cache.stub(:fields_method) { :safe }
24
+ cache.stub(:fields) { ["a", "b"] }
25
+ resource = Object.new
26
+ resource.stub(:changed) { ["a", "b"] }
27
+ DummyClass.new.expire_cache?(cache, resource).should be_false
28
+ end
29
+ end
30
+
31
+ context "trigger fields" do
32
+ it "returns true when some of the trigger fields changed" do
33
+ cache = Object.new
34
+ cache.stub(:fields_method) { :trigger }
35
+ cache.stub(:fields) { ["a"] }
36
+ resource = Object.new
37
+ resource.stub(:changed) { ["a", "b", "c"] }
38
+ DummyClass.new.expire_cache?(cache, resource).should be_true
39
+ end
40
+
41
+ it "returns false when none of the trigger fields changed" do
42
+ cache = Object.new
43
+ cache.stub(:fields_method) { :trigger }
44
+ cache.stub(:fields) { ["x", "y"] }
45
+ resource = Object.new
46
+ resource.stub(:changed) { ["a", "b", "c"] }
47
+ DummyClass.new.expire_cache?(cache, resource).should be_false
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: custom_fragment_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yarin Goldman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Define your custom fragment caches
84
+ email:
85
+ - yarin@gettaxi.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .DS_Store
91
+ - .gitignore
92
+ - .rspec
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - custom_fragment_cache.gemspec
98
+ - lib/custom_fragment_cache.rb
99
+ - lib/custom_fragment_cache/cache_helper_method.rb
100
+ - lib/custom_fragment_cache/configuration.rb
101
+ - lib/custom_fragment_cache/fragment.rb
102
+ - lib/custom_fragment_cache/helper.rb
103
+ - lib/custom_fragment_cache/logic.rb
104
+ - lib/custom_fragment_cache/railtie.rb
105
+ - lib/custom_fragment_cache/version.rb
106
+ - spec/custom_fragment_cache/logic_spec.rb
107
+ - spec/spec_helper.rb
108
+ homepage: ''
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.3.0
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Define your custom fragment caches
132
+ test_files:
133
+ - spec/custom_fragment_cache/logic_spec.rb
134
+ - spec/spec_helper.rb
135
+ has_rdoc: