munna 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 18d02948e6426b1492c72e4856414458efbdc506
4
- data.tar.gz: 10ffc7cb7b37cac78ec12af8d841b746fbe0adc2
3
+ metadata.gz: 5fee768a4cbf01ff3d9cf98b7dec7e3d562507cf
4
+ data.tar.gz: 08ea8ad87025d10c47e9cffe7cac07c58fd01cd3
5
5
  SHA512:
6
- metadata.gz: 7b2d9b24b2f4512e638b0786c53a9777aeb9c2771a7194cf2daa0cc150ecab1e5b6b9f2b9d2ce9e0503bbebf05e369d89a536b4e425e97a1c71271ea9918df3c
7
- data.tar.gz: fc087b37ee4d7220e9b3471e96fdd4b1728f0485487a7c1225f5f1c5027a6f70547f895a41a75658d5bff8b48549078460e3d386dd249866c5313fbf57a77e24
6
+ metadata.gz: 6a3af5676fcca9b5d80e4fa0c7fa100352166104c8de089665027e47c342b3af9df5c32356f5a345b36ac17ab7f17d2948263e8ea2cf40fd74b4f268f86111c5
7
+ data.tar.gz: 96d65fc8852af8f3672c4df59d551c59ef72703593bc04310889faf9867473b7552316e80bcbe49637fc86202f71146c22a950931070958542ddcfe98b074abc
data/Gemfile CHANGED
@@ -5,18 +5,18 @@ gemspec
5
5
  gem 'rails'
6
6
 
7
7
  group :test do
8
- gem 'coveralls', :require => false
9
- gem 'rack-test'
10
- gem 'rspec'
11
- gem 'rspec-rails'
12
- gem 'simplecov', :require => false
8
+ gem 'coveralls', :require => false
9
+ gem 'rack-test'
10
+ gem 'rspec'
11
+ gem 'rspec-rails'
12
+ gem 'simplecov', :require => false
13
13
  end
14
14
 
15
15
  group :development, :test do
16
- gem 'debugger'
17
- gem 'pry'
18
- gem 'pry-debugger'
19
- gem 'pry-stack_explorer'
20
- gem 'pry-rails'
21
- gem 'pry-remote'
16
+ gem 'debugger'
17
+ gem 'pry'
18
+ gem 'pry-debugger'
19
+ gem 'pry-stack_explorer'
20
+ gem 'pry-rails'
21
+ gem 'pry-remote'
22
22
  end
data/README.md CHANGED
@@ -6,7 +6,7 @@ Simple cache wrapper for rails ActiveRecord, Object, ... to speed up your site
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
- gem 'munna'
9
+ gem 'munna'
10
10
 
11
11
  ### Usage
12
12
 
@@ -14,28 +14,30 @@ Add this line to your application's Gemfile:
14
14
 
15
15
  ``` ruby
16
16
  class Profile < ActiveRecord::Base
17
- after_destroy :clear_cache
17
+ after_destroy :clear_cache
18
18
 
19
- def clear_cache
20
- User.delete_cached :list_with_profiles
21
- end
19
+ def clear_cache
20
+ User.delete_cached :list_with_profiles
21
+ end
22
22
  end
23
23
 
24
24
  class User < ActiveRecord::Base
25
- has_many :profiles
26
-
27
- def self.list_with_profiles
28
- cache.join(:profiles)
29
- .group('users.id')
30
- .select('users.*, count(users.id)')
31
- end
32
-
33
- # get random 5 rows, cache for 10.hours
34
- def self.list_by_random
35
- cached_for 10.hours {
36
- order('rand(id)').limit(5)
37
- }
38
- end
25
+ has_many :profiles
26
+
27
+ def self.list_with_profiles
28
+ cached {
29
+ join(:profiles)
30
+ .group('users.id')
31
+ .select('users.*, count(users.id)')
32
+ }
33
+ end
34
+
35
+ # get random 5 rows, cache for 10.hours
36
+ def self.list_by_random
37
+ cached_for 10.hours {
38
+ order('rand(id)').limit(5)
39
+ }
40
+ end
39
41
  end
40
42
  ```
41
43
 
@@ -48,10 +50,10 @@ Everything in rails can be cached through **cached**, **cached_for** and remove
48
50
 
49
51
  ##### 1. cached vs delete_cached
50
52
 
51
- | cached | delete_cached |
53
+ | cached | delete_cached |
52
54
  | -------------------------------------------- | ----------------------------------- |
53
- | Anything.cached(opts).function | Anything.delete_cached.function |
54
- | Anything.cached(:name, opts).function | Anything.delete_cached :name |
55
+ | Anything.cached(opts).function | Anything.delete_cached.function |
56
+ | Anything.cached(:name, opts).function | Anything.delete_cached :name |
55
57
  | Anything.cached([:name, :id], opts).function | Anything.delete_cached [:name, :id] |
56
58
  | Anything.cached {} | Anything.delete_cached caller_name |
57
59
  | Anything.cached(:name, opts) { ... } | Anything.delete_cached :name |
@@ -68,8 +70,8 @@ Anything.cached_for(10.hours, opts) {}
68
70
  * ***caller_name*** is the function name that line belong to
69
71
 
70
72
  ##### 3. opts: is a ```Hash```, has options
71
- * expires_in: time to expire
72
- * key: cached key for save and use for ***delete_cached***
73
+ * expires_in: time to expire
74
+ * key: cached key for save and use for ***delete_cached***
73
75
 
74
76
  #### 4. cache support
75
77
  ``` ruby
data/lib/munna.rb CHANGED
@@ -4,37 +4,37 @@ require 'munna/cache'
4
4
 
5
5
  module Munna
6
6
 
7
- class << self
8
- def cache
9
- Cache.instance
10
- end
7
+ class << self
8
+ def cache
9
+ Cache.instance
10
+ end
11
11
 
12
- def get_key(params)
13
- if [Symbol, String].include? params.class
14
- params
15
- else
16
- (params.map {|v| v.is_a?(Array) ? v.join('-') : v}).join('/')
17
- end
18
- end
19
- end
12
+ def get_key(params)
13
+ if [Symbol, String].include? params.class
14
+ params
15
+ else
16
+ (params.map {|v| v.is_a?(Array) ? v.join('-') : v}).join('/')
17
+ end
18
+ end
19
+ end
20
20
 
21
- KEY = 'Munna'
21
+ KEY = 'Munna'
22
22
 
23
- module Extensions
24
- require 'munna/extensions/helper'
25
- require 'munna/extensions/basic_method'
26
- require 'munna/extensions/object_method'
27
- require 'munna/extensions/activerecord_method'
28
- end
23
+ module Extensions
24
+ require 'munna/extensions/helper'
25
+ require 'munna/extensions/basic_method'
26
+ require 'munna/extensions/object_method'
27
+ require 'munna/extensions/activerecord_method'
28
+ end
29
29
 
30
- module Proxy
31
- require 'munna/proxy/base'
32
- require 'munna/proxy/execute'
33
- end
30
+ module Proxy
31
+ require 'munna/proxy/base'
32
+ require 'munna/proxy/execute'
33
+ end
34
34
 
35
- module Performs
36
- require 'munna/perform/base'
37
- require 'munna/perform/object'
38
- require 'munna/perform/active_record'
39
- end
35
+ module Performs
36
+ require 'munna/perform/base'
37
+ require 'munna/perform/object'
38
+ require 'munna/perform/active_record'
39
+ end
40
40
  end
data/lib/munna/cache.rb CHANGED
@@ -1,67 +1,67 @@
1
1
  require 'singleton'
2
2
 
3
3
  module Munna
4
- class Cache
5
- include Singleton
4
+ class Cache
5
+ include Singleton
6
6
 
7
- def initialize
8
- values
9
- end
7
+ def initialize
8
+ values
9
+ end
10
10
 
11
- def values
12
- Rails.cache.fetch(Munna::KEY) { Hash.new }
13
- end
11
+ def values
12
+ Rails.cache.fetch(Munna::KEY) { Hash.new }
13
+ end
14
14
 
15
- def clear
16
- values.keys.each {|k| Rails.cache.delete k }
17
- Rails.cache.delete(Munna::KEY)
18
- end
15
+ def clear
16
+ values.keys.each {|k| Rails.cache.delete k }
17
+ Rails.cache.delete(Munna::KEY)
18
+ end
19
19
 
20
- def method_missing(name, *args, &block)
21
- if /^write$|^delete$/ =~ name.to_s
22
- CacheHelper.handle_key(name, args.first)
23
- elsif /(?<type>.+)_matched$/ =~ name.to_s
24
- CacheHelper.handle_matched(type, args.first)
25
- else
26
- super(name, *args, &block)
27
- end
28
- end
29
- end
20
+ def method_missing(name, *args, &block)
21
+ if /^write$|^delete$/ =~ name.to_s
22
+ CacheHelper.handle_key(name, args.first)
23
+ elsif /(?<type>.+)_matched$/ =~ name.to_s
24
+ CacheHelper.handle_matched(type, args.first)
25
+ else
26
+ super(name, *args, &block)
27
+ end
28
+ end
29
+ end
30
30
 
31
- module CacheHelper
32
- class << self
33
- def handle_matched(action, regex)
34
- matches = Munna.cache.values.select {|v| regex =~ v}
35
- send("#{action}_matched", matches)
36
- end
31
+ module CacheHelper
32
+ class << self
33
+ def handle_matched(action, regex)
34
+ matches = Munna.cache.values.select {|v| regex =~ v}
35
+ send("#{action}_matched", matches)
36
+ end
37
37
 
38
- def delete_matched(matches)
39
- matches.each {|m| Munna.cache.delete(m)}
40
- end
38
+ def delete_matched(matches)
39
+ matches.each {|k, v| Munna.cache.delete(k)}
40
+ end
41
41
 
42
- def get_matched(matches)
43
- matches
44
- end
42
+ def get_matched(matches)
43
+ matches
44
+ end
45
45
 
46
- def handle_key(action, key)
47
- Rails.cache.write(Munna::KEY, send("#{action}_key", Munna.cache.values, key))
48
- end
46
+ def handle_key(action, key)
47
+ Rails.cache.write(Munna::KEY, send("#{action}_key", Munna.cache.values, key))
48
+ end
49
49
 
50
- def write_key(values, key)
51
- values[key] = values[key].to_i + 1 and values
52
- end
50
+ def write_key(values, key)
51
+ values[key] = values[key].to_i + 1 and values
52
+ end
53
53
 
54
- def delete_key(values, key)
55
- values.reject do |v|
56
- if normailize_value(v) == normailize_value(key)
57
- Rails.cache.delete(v) and true
58
- end
59
- end
60
- end
54
+ def delete_key(values, key)
55
+ values.reject do |k, v|
56
+ if normailize_value(k) == normailize_value(key)
57
+ Rails.cache.delete(k) and true
58
+ end
59
+ end
60
+ end
61
61
 
62
- def normailize_value(key)
63
- key.gsub /-[^\/]+/, ''
64
- end
65
- end
66
- end
62
+ def normailize_value(key)
63
+ key.gsub /-[^\/]+/, ''
64
+ end
65
+ end
66
+ end
67
67
  end
@@ -1,13 +1,13 @@
1
1
  module Munna
2
- module Extensions
3
- module ActiveRecord
4
- include Basic
2
+ module Extensions
3
+ module ActiveRecord
4
+ include Basic
5
5
 
6
- def munna_perform
7
- Perform::ActiveRecord
8
- end
9
- end
10
- end
6
+ def munna_perform
7
+ Perform::ActiveRecord
8
+ end
9
+ end
10
+ end
11
11
  end
12
12
 
13
13
  ActiveRecord::Base.send(:include, Munna::Extensions::ActiveRecord)
@@ -1,17 +1,17 @@
1
1
  module Munna
2
- module Extensions
3
- module Basic
4
- def cached(*opts, &block)
5
- Proxy::Write.builder(munna_perform, self, Helper.normalize_params(opts), &block)
6
- end
2
+ module Extensions
3
+ module Basic
4
+ def cached(*opts, &block)
5
+ Proxy::Write.builder(munna_perform, self, Helper.normalize_params(opts), &block)
6
+ end
7
7
 
8
- def cached_for(expires_in, *opts, &block)
9
- Proxy::Write.builder(munna_perform, self, Helper.normalize_params(opts).merge(:expires_in => expires_in), &block)
10
- end
8
+ def cached_for(expires_in, *opts, &block)
9
+ Proxy::Write.builder(munna_perform, self, Helper.normalize_params(opts).merge(:expires_in => expires_in), &block)
10
+ end
11
11
 
12
- def delete_cached(*opts)
13
- Proxy::Delete.builder(munna_perform, self, Helper.normalize_params(opts))
14
- end
15
- end
16
- end
12
+ def delete_cached(*opts)
13
+ Proxy::Delete.builder(munna_perform, self, Helper.normalize_params(opts))
14
+ end
15
+ end
16
+ end
17
17
  end
@@ -1,18 +1,18 @@
1
1
  module Munna
2
- module Extensions
3
- module Helper
4
- def self.normalize_params(params)
5
- first_element = params.first
6
- last_element = params.last
2
+ module Extensions
3
+ module Helper
4
+ def self.normalize_params(params)
5
+ first_element = params.first
6
+ last_element = params.last
7
7
 
8
- cache_opts = last_element.is_a?(Hash) ? last_element : Hash.new
9
- cache_opts[:caller_name] = caller_locations(2,1)[0].label
10
- if [Symbol, String, Array].include? first_element.class
11
- cache_opts[:key] = Munna.get_key first_element
12
- end
8
+ cache_opts = last_element.is_a?(Hash) ? last_element : Hash.new
9
+ cache_opts[:caller_name] = caller_locations(2,1)[0].label
10
+ if [Symbol, String, Array].include? first_element.class
11
+ cache_opts[:key] = Munna.get_key first_element
12
+ end
13
13
 
14
- cache_opts
15
- end
16
- end
17
- end
14
+ cache_opts
15
+ end
16
+ end
17
+ end
18
18
  end
@@ -1,17 +1,17 @@
1
1
  module Munna
2
- module Extensions
3
- module Object
4
- include Basic
2
+ module Extensions
3
+ module Object
4
+ include Basic
5
5
 
6
- def cached_key
7
- Munna.get_key [self.class, object_id]
8
- end
6
+ def cached_key
7
+ Munna.get_key [self.class, object_id]
8
+ end
9
9
 
10
- def munna_perform
11
- Perform::Object
12
- end
13
- end
14
- end
10
+ def munna_perform
11
+ Perform::Object
12
+ end
13
+ end
14
+ end
15
15
  end
16
16
 
17
17
  Object.send(:include, Munna::Extensions::Object)
@@ -1,35 +1,35 @@
1
1
  module Munna
2
- module Perform
3
- class ActiveRecord < Base
4
- def target_name
5
- @target.class == Class ? @target.table_name : @target.cache_key
6
- end
2
+ module Perform
3
+ class ActiveRecord < Base
4
+ def target_name
5
+ @target.class == Class ? @target.table_name : @target.cache_key
6
+ end
7
7
 
8
- def method_name
9
- [super, (send(@execute.name) rescue nil)].compact
10
- end
8
+ def method_name
9
+ [super, (send(@execute.name) rescue nil)].compact
10
+ end
11
11
 
12
- def normalize(values)
13
- values.is_a?(::ActiveRecord::Relation) ? values.to_a : values
14
- end
12
+ def normalize(values)
13
+ values.is_a?(::ActiveRecord::Relation) ? values.to_a : values
14
+ end
15
15
 
16
- def method_missing(name, *args, &block)
17
- if /^all$/ =~ name.to_s
18
- ActiveRecordMethod.new(@target).send(name)
19
- else
20
- super(name, *args, &block)
21
- end
22
- end
23
- end
16
+ def method_missing(name, *args, &block)
17
+ if /^all$/ =~ name.to_s
18
+ ActiveRecordMethod.new(@target).send(name)
19
+ else
20
+ super(name, *args, &block)
21
+ end
22
+ end
23
+ end
24
24
 
25
- class ActiveRecordMethod
26
- def initialize(target)
27
- @target = target
28
- end
25
+ class ActiveRecordMethod
26
+ def initialize(target)
27
+ @target = target
28
+ end
29
29
 
30
- def all
31
- @target.maximum(:updated_at).to_i
32
- end
33
- end
34
- end
30
+ def all
31
+ @target.maximum(:updated_at).to_i
32
+ end
33
+ end
34
+ end
35
35
  end
@@ -1,34 +1,34 @@
1
1
  module Munna
2
- module Perform
3
- class Base
4
- def initialize(target, opts, execute)
5
- @target = target
6
- @opts = opts
7
- @execute = execute
8
- end
2
+ module Perform
3
+ class Base
4
+ def initialize(target, opts, execute)
5
+ @target = target
6
+ @opts = opts
7
+ @execute = execute
8
+ end
9
9
 
10
- def perform_write
11
- Munna.cache.write cached_key
12
- Rails.cache.fetch cached_key, @opts do
13
- normalize @execute.value
14
- end
15
- end
10
+ def perform_write
11
+ Munna.cache.write cached_key
12
+ Rails.cache.fetch cached_key, @opts do
13
+ normalize @execute.value
14
+ end
15
+ end
16
16
 
17
- def perform_delete
18
- Munna.cache.delete cached_key
19
- end
17
+ def perform_delete
18
+ Munna.cache.delete cached_key
19
+ end
20
20
 
21
- def cached_key
22
- @cached_key ||= Munna.get_key [target_name, method_name]
23
- end
21
+ def cached_key
22
+ @cached_key ||= Munna.get_key [target_name, method_name]
23
+ end
24
24
 
25
- def method_name
26
- @opts[:key] || @execute.name || @opts[:caller_name]
27
- end
25
+ def method_name
26
+ @opts[:key] || @execute.name || @opts[:caller_name]
27
+ end
28
28
 
29
- def normalize(value)
30
- value
31
- end
32
- end
33
- end
29
+ def normalize(value)
30
+ value
31
+ end
32
+ end
33
+ end
34
34
  end
@@ -1,9 +1,9 @@
1
1
  module Munna
2
- module Perform
3
- class Object < Base
4
- def target_name
5
- @target.class.is_a?(Class) ? @target.name : @target.cached_key
6
- end
7
- end
8
- end
2
+ module Perform
3
+ class Object < Base
4
+ def target_name
5
+ @target.class.is_a?(Class) ? @target.name : @target.cached_key
6
+ end
7
+ end
8
+ end
9
9
  end
@@ -1,33 +1,33 @@
1
1
  module Munna
2
- module Proxy
3
- class Base
4
- def initialize(kclass, target, options={}, &block)
5
- @kclass = kclass
6
- @target = target
7
- @opts = options
8
- end
2
+ module Proxy
3
+ class Base
4
+ def initialize(kclass, target, options={}, &block)
5
+ @kclass = kclass
6
+ @target = target
7
+ @opts = options
8
+ end
9
9
 
10
- def method_missing(name, *args, &block)
11
- __perform__ Execute.new @target, {:name => name, :args => args, :block => block}
12
- end
10
+ def method_missing(name, *args, &block)
11
+ __perform__ Execute.new @target, {:name => name, :args => args, :block => block}
12
+ end
13
13
 
14
- def __perform__(execute)
15
- @kclass.new(@target, @opts, execute).send("perform_#{self.class.name.demodulize.downcase}")
16
- end
17
- end
14
+ def __perform__(execute)
15
+ @kclass.new(@target, @opts, execute).send("perform_#{self.class.name.demodulize.downcase}")
16
+ end
17
+ end
18
18
 
19
- class Write < Base
20
- def self.builder(kclass, target, options={}, &block)
21
- instance = new kclass, target, options, &block
22
- block.present? ? instance.__perform__(Execute.new target, block) : instance
23
- end
24
- end
19
+ class Write < Base
20
+ def self.builder(kclass, target, options={}, &block)
21
+ instance = new kclass, target, options, &block
22
+ block.present? ? instance.__perform__(Execute.new target, block) : instance
23
+ end
24
+ end
25
25
 
26
- class Delete < Base
27
- def self.builder(kclass, target, options={}, &block)
28
- instance = new kclass, target, options, &block
29
- options[:key].present? ? instance.__perform__(Execute.new target) : instance
30
- end
31
- end
32
- end
26
+ class Delete < Base
27
+ def self.builder(kclass, target, options={}, &block)
28
+ instance = new kclass, target, options, &block
29
+ options[:key].present? ? instance.__perform__(Execute.new target) : instance
30
+ end
31
+ end
32
+ end
33
33
  end
@@ -1,22 +1,22 @@
1
1
  module Munna
2
- module Proxy
3
- class Execute
4
- def initialize(target, object={})
5
- @target = target
6
- @object = object
7
- end
2
+ module Proxy
3
+ class Execute
4
+ def initialize(target, object={})
5
+ @target = target
6
+ @object = object
7
+ end
8
8
 
9
- def name
10
- @object.is_a?(Proc) ? nil : @object[:name]
11
- end
9
+ def name
10
+ @object.is_a?(Proc) ? nil : @object[:name]
11
+ end
12
12
 
13
- def value
14
- @value ||= if @object.is_a? Proc
15
- @object.call
16
- else
17
- @target.send @object[:name], *@object[:params]
18
- end
19
- end
20
- end
21
- end
13
+ def value
14
+ @value ||= if @object.is_a? Proc
15
+ @object.call
16
+ else
17
+ @target.send @object[:name], *@object[:params]
18
+ end
19
+ end
20
+ end
21
+ end
22
22
  end
data/lib/munna/railtie.rb CHANGED
@@ -3,7 +3,7 @@ require 'rails'
3
3
  module Munna
4
4
  class Railtie < Rails::Railtie
5
5
  rake_tasks do
6
- Dir.glob("#{File.dirname(__FILE__)}/../rake/*.rake", &method(:load))
6
+ Dir.glob("#{File.dirname(__FILE__)}/../rake/*.rake", &method(:load))
7
7
  end
8
8
  end
9
9
  end
data/lib/munna/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Munna
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/rake/munna.rake CHANGED
@@ -5,6 +5,6 @@ namespace :munna do
5
5
  end
6
6
 
7
7
  task :values => :environment do
8
- puts Munna.cache.values
8
+ puts Munna.cache.values
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: munna
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - |
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-22 00:00:00.000000000 Z
12
+ date: 2014-07-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -102,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  version: '0'
103
103
  requirements: []
104
104
  rubyforge_project:
105
- rubygems_version: 2.2.1
105
+ rubygems_version: 2.2.2
106
106
  signing_key:
107
107
  specification_version: 4
108
108
  summary: Add cache for your object