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 +4 -4
- data/Gemfile +11 -11
- data/README.md +26 -24
- data/lib/munna.rb +28 -28
- data/lib/munna/cache.rb +52 -52
- data/lib/munna/extensions/activerecord_method.rb +8 -8
- data/lib/munna/extensions/basic_method.rb +13 -13
- data/lib/munna/extensions/helper.rb +14 -14
- data/lib/munna/extensions/object_method.rb +11 -11
- data/lib/munna/perform/active_record.rb +28 -28
- data/lib/munna/perform/base.rb +27 -27
- data/lib/munna/perform/object.rb +7 -7
- data/lib/munna/proxy/base.rb +27 -27
- data/lib/munna/proxy/execute.rb +18 -18
- data/lib/munna/railtie.rb +1 -1
- data/lib/munna/version.rb +1 -1
- data/lib/rake/munna.rake +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5fee768a4cbf01ff3d9cf98b7dec7e3d562507cf
|
4
|
+
data.tar.gz: 08ea8ad87025d10c47e9cffe7cac07c58fd01cd3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
-
|
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
|
-
|
17
|
+
after_destroy :clear_cache
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
53
|
+
| cached | delete_cached |
|
52
54
|
| -------------------------------------------- | ----------------------------------- |
|
53
|
-
| Anything.cached(opts).function
|
54
|
-
| Anything.cached(:name, opts).function
|
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
|
-
|
72
|
-
|
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
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
class << self
|
8
|
+
def cache
|
9
|
+
Cache.instance
|
10
|
+
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
21
|
+
KEY = 'Munna'
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
module Proxy
|
31
|
+
require 'munna/proxy/base'
|
32
|
+
require 'munna/proxy/execute'
|
33
|
+
end
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
-
|
5
|
-
|
4
|
+
class Cache
|
5
|
+
include Singleton
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
def initialize
|
8
|
+
values
|
9
|
+
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
def values
|
12
|
+
Rails.cache.fetch(Munna::KEY) { Hash.new }
|
13
|
+
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
def clear
|
16
|
+
values.keys.each {|k| Rails.cache.delete k }
|
17
|
+
Rails.cache.delete(Munna::KEY)
|
18
|
+
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
39
|
-
|
40
|
-
|
38
|
+
def delete_matched(matches)
|
39
|
+
matches.each {|k, v| Munna.cache.delete(k)}
|
40
|
+
end
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
|
42
|
+
def get_matched(matches)
|
43
|
+
matches
|
44
|
+
end
|
45
45
|
|
46
|
-
|
47
|
-
|
48
|
-
|
46
|
+
def handle_key(action, key)
|
47
|
+
Rails.cache.write(Munna::KEY, send("#{action}_key", Munna.cache.values, key))
|
48
|
+
end
|
49
49
|
|
50
|
-
|
51
|
-
|
52
|
-
|
50
|
+
def write_key(values, key)
|
51
|
+
values[key] = values[key].to_i + 1 and values
|
52
|
+
end
|
53
53
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
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
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
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
|
-
|
3
|
-
|
4
|
-
|
2
|
+
module Extensions
|
3
|
+
module ActiveRecord
|
4
|
+
include Basic
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
cache_opts
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
18
|
end
|
@@ -1,17 +1,17 @@
|
|
1
1
|
module Munna
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
module Extensions
|
3
|
+
module Object
|
4
|
+
include Basic
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
def cached_key
|
7
|
+
Munna.get_key [self.class, object_id]
|
8
|
+
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
8
|
+
def method_name
|
9
|
+
[super, (send(@execute.name) rescue nil)].compact
|
10
|
+
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
def normalize(values)
|
13
|
+
values.is_a?(::ActiveRecord::Relation) ? values.to_a : values
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
class ActiveRecordMethod
|
26
|
+
def initialize(target)
|
27
|
+
@target = target
|
28
|
+
end
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
def all
|
31
|
+
@target.maximum(:updated_at).to_i
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
35
|
end
|
data/lib/munna/perform/base.rb
CHANGED
@@ -1,34 +1,34 @@
|
|
1
1
|
module Munna
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
17
|
+
def perform_delete
|
18
|
+
Munna.cache.delete cached_key
|
19
|
+
end
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
def cached_key
|
22
|
+
@cached_key ||= Munna.get_key [target_name, method_name]
|
23
|
+
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
def method_name
|
26
|
+
@opts[:key] || @execute.name || @opts[:caller_name]
|
27
|
+
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
29
|
+
def normalize(value)
|
30
|
+
value
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
34
|
end
|
data/lib/munna/perform/object.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
module Munna
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
data/lib/munna/proxy/base.rb
CHANGED
@@ -1,33 +1,33 @@
|
|
1
1
|
module Munna
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
11
|
-
|
12
|
-
|
10
|
+
def method_missing(name, *args, &block)
|
11
|
+
__perform__ Execute.new @target, {:name => name, :args => args, :block => block}
|
12
|
+
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
def __perform__(execute)
|
15
|
+
@kclass.new(@target, @opts, execute).send("perform_#{self.class.name.demodulize.downcase}")
|
16
|
+
end
|
17
|
+
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
data/lib/munna/proxy/execute.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
module Munna
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
2
|
+
module Proxy
|
3
|
+
class Execute
|
4
|
+
def initialize(target, object={})
|
5
|
+
@target = target
|
6
|
+
@object = object
|
7
|
+
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
def name
|
10
|
+
@object.is_a?(Proc) ? nil : @object[:name]
|
11
|
+
end
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
data/lib/munna/version.rb
CHANGED
data/lib/rake/munna.rake
CHANGED
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.
|
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-
|
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.
|
105
|
+
rubygems_version: 2.2.2
|
106
106
|
signing_key:
|
107
107
|
specification_version: 4
|
108
108
|
summary: Add cache for your object
|