method_cachable 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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MGIyMmYxNmQyOGI0ZGUxYWNhNDA5YzYyY2M1YzUxN2NmMTZhOTI5Zg==
5
+ data.tar.gz: !binary |-
6
+ ZjQ0OTFlYmU4ZTQwMDZlMjE0NWEyYTdmZmIxNjBlZDdhZmZlMTA4NA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NjdkNWI2MjEwN2YzOTAwMjRhZjM1NWJmMDJjOTdjNWRlYjFhYmMyZTg3MzI3
10
+ Mzg5OTdhMmI3ZTM5ZWYwZGY2MDU4YmY4NGY5ZWYwMzM1YmYwMzIwYzUwZDc1
11
+ ODJhZWMyODNmMGFjMDFiZDhlNDFlNGQyOTA1NmI4MDdmOTU5YzM=
12
+ data.tar.gz: !binary |-
13
+ MDMxYjNjNWEyMWE4Mzc0YmI1ZGYyNmNkYmNhYjc3Y2I1MmYwNzUwOTM0Zjc0
14
+ NzA3ZGU5N2IxMzIyOWExYWIwZjJhZmQ1NzNjNjMyNTgwYTkyMmU5NWJjODlm
15
+ ZmI3NjY5NWE5Zjc4NjBjMGZjZjNiYjY0NjRhNDQyMjk2OTg3YWI=
data/.gitignore ADDED
@@ -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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in method_cachable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 sergio1990
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,80 @@
1
+ # MethodCachable
2
+
3
+ Provides setting specific methods list that must be cached and clearing cache on CRUD operations.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'method_cachable'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install method_cachable
20
+
21
+ ## Usage
22
+
23
+ ### Basic configuration
24
+
25
+ ```ruby
26
+ class Post < ActiveRecord::Base
27
+ include MethodCachable
28
+
29
+ belongs_to :menu_item
30
+
31
+ # Add this line for callbacks working
32
+ acts_as_cachable
33
+
34
+ # You want to cache association calling...
35
+ def menu_item_title
36
+ self.menu_item.title
37
+ end
38
+
39
+ # ...or maybe some evaluations
40
+ def some_evaluations
41
+ # There must be your code
42
+ end
43
+
44
+ # Specify list of cachable methods.
45
+ cached_methods :menu_item_title, :some_evaluations
46
+ end
47
+ ```
48
+
49
+ **_PLEASE NOTE_**: after save/touch/destroy cache will expire automatically.
50
+
51
+ ### Cache storage
52
+
53
+ For storing cached methods gem using *Rails.cache*. Therefore you must set in rails application config file *cache_store* option. I highly recommend using the couple of *memcached*+*dalli* because they work very quickly. For this, you must add gem dalli to your Gemfile:
54
+
55
+ ```ruby
56
+ gem 'dalli'
57
+ ```
58
+
59
+ And then configurate it in *production.rb*:
60
+
61
+ ```ruby
62
+ YourApp::Application.configure do
63
+ config.cache_store = :dalli_store, { namespace: :world_try, expires_in: 2.hours, compress: true }
64
+ # Rest of configuration
65
+ end
66
+ ```
67
+
68
+ **_PLEASE NOTE_**: you don't need to configurate cache_store in development mode because for convenient development caching is off and method is called directly.
69
+
70
+ ## Future developing
71
+
72
+ Now you may to cache only instance methods but caching class methods gem doesn't allow. In future, I will implement this functionality.
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,32 @@
1
+ module MethodCachable
2
+ module Helper
3
+
4
+ def createsig(body)
5
+ Digest::MD5.hexdigest( sigflat body )
6
+ end
7
+
8
+ def sigflat(body)
9
+ res = nil
10
+ if body.class == Hash
11
+ arr = []
12
+ body.each do |key, value|
13
+ arr << "#{sigflat key}=>#{sigflat value}"
14
+ end
15
+ res = arr
16
+ end
17
+ if body.class == Array
18
+ str = ''
19
+ res = body.map do |value|
20
+ sigflat value
21
+ end.sort!.each do |value|
22
+ str << value
23
+ end
24
+ end
25
+ if body.class != String
26
+ res = body.to_s << body.class.to_s
27
+ end
28
+ res || body
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module MethodCachable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,71 @@
1
+ require "method_cachable/version"
2
+ require "method_cachable/helper"
3
+
4
+ module MethodCachable
5
+
6
+ include MethodCachable::Helper
7
+
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ def clear_cache
12
+ flush_cache cache_list_key
13
+ flush_cache "cache_list_key:#{self.class.name}"
14
+ flush_cache "cache_list_key:#{self.class.superclass.name}"
15
+ end
16
+
17
+ def cache_list_key
18
+ "cache_list_key:#{self.class.name}:#{self.id}"
19
+ end
20
+ end
21
+
22
+ module ClassMethods
23
+
24
+ def acts_as_cachable
25
+ klass = self
26
+ [:after_touch, :before_destroy, :after_save].each { |callback| klass.send(callback, :clear_cache) }
27
+ end
28
+
29
+ def cached_methods(*methods)
30
+ name = self.name
31
+ eval_string = ""
32
+ methods.each do |method|
33
+ method = method.to_s
34
+ eval_string += "
35
+ alias_method :old_#{method}, :#{method}
36
+
37
+ def #{method}(*args)
38
+ sig = createsig(args || self.id)
39
+ with_env \"#{name}:#{method}:\#{self.id}:\#{sig}\", cache_list_key do
40
+ old_#{method}(*args)
41
+ end
42
+ end
43
+ "
44
+ end
45
+ class_eval(eval_string)
46
+ end
47
+
48
+ end
49
+
50
+ def with_env(key, saving_key = nil, &block)
51
+ if Rails.env.production?
52
+ unless saving_key.nil?
53
+ lst = Rails.cache.fetch saving_key do; [] end
54
+ lst << key unless lst.include?(saving_key)
55
+ Rails.cache.write saving_key, lst, expires_in: 24.hours
56
+ end
57
+ Rails.cache.fetch key, expires_in: 2.hours, &block
58
+ else
59
+ yield
60
+ end
61
+ end
62
+
63
+ def flush_cache(saving_key)
64
+ lst = Rails.cache.fetch saving_key do; [] end
65
+ lst.each {|key| flush_key(key) }
66
+ end
67
+
68
+ def flush_key(key)
69
+ Rails.cache.delete key
70
+ end
71
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'method_cachable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "method_cachable"
8
+ spec.version = MethodCachable::VERSION
9
+ spec.authors = ["Sergey Gernyak"]
10
+ spec.email = ["sergeg1990@gmail.com"]
11
+ spec.description = %q{Intelligent method caching}
12
+ spec.summary = %q{Provides setting specific methods list that must be cached and clearing cache on CRUD operations}
13
+ spec.homepage = "http://github.com/sergio1990/method_cachable"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'activerecord'
22
+ spec.add_dependency 'activesupport'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: method_cachable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Gernyak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ! '>='
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ name: activerecord
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ name: activesupport
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '1.3'
47
+ type: :development
48
+ prerelease: false
49
+ name: bundler
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :development
62
+ prerelease: false
63
+ name: rake
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Intelligent method caching
70
+ email:
71
+ - sergeg1990@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/method_cachable.rb
82
+ - lib/method_cachable/helper.rb
83
+ - lib/method_cachable/version.rb
84
+ - method_cachable.gemspec
85
+ homepage: http://github.com/sergio1990/method_cachable
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.0.6
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Provides setting specific methods list that must be cached and clearing cache
109
+ on CRUD operations
110
+ test_files: []