munna 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/README.md +66 -15
- data/lib/munna/cache.rb +1 -1
- data/lib/munna/extensions/basic_method.rb +2 -2
- data/lib/munna/perform/base.rb +2 -2
- data/lib/munna/proxy/base.rb +6 -6
- data/lib/munna/version.rb +1 -1
- data/lib/munna.rb +4 -8
- data/spec/spec_helper.rb +17 -0
- metadata +16 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 18d02948e6426b1492c72e4856414458efbdc506
|
4
|
+
data.tar.gz: 10ffc7cb7b37cac78ec12af8d841b746fbe0adc2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b2d9b24b2f4512e638b0786c53a9777aeb9c2771a7194cf2daa0cc150ecab1e5b6b9f2b9d2ce9e0503bbebf05e369d89a536b4e425e97a1c71271ea9918df3c
|
7
|
+
data.tar.gz: fc087b37ee4d7220e9b3471e96fdd4b1728f0485487a7c1225f5f1c5027a6f70547f895a41a75658d5bff8b48549078460e3d386dd249866c5313fbf57a77e24
|
data/.rspec
ADDED
data/README.md
CHANGED
@@ -1,29 +1,80 @@
|
|
1
1
|
# Munna
|
2
2
|
|
3
|
-
|
3
|
+
Simple cache wrapper for rails ActiveRecord, Object, ... to speed up your site
|
4
4
|
|
5
|
-
|
5
|
+
### Installation
|
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
|
|
13
|
-
|
13
|
+
#### ActiveRecord
|
14
14
|
|
15
|
-
|
15
|
+
``` ruby
|
16
|
+
class Profile < ActiveRecord::Base
|
17
|
+
after_destroy :clear_cache
|
16
18
|
|
17
|
-
|
19
|
+
def clear_cache
|
20
|
+
User.delete_cached :list_with_profiles
|
21
|
+
end
|
22
|
+
end
|
18
23
|
|
19
|
-
|
24
|
+
class User < ActiveRecord::Base
|
25
|
+
has_many :profiles
|
20
26
|
|
21
|
-
|
27
|
+
def self.list_with_profiles
|
28
|
+
cache.join(:profiles)
|
29
|
+
.group('users.id')
|
30
|
+
.select('users.*, count(users.id)')
|
31
|
+
end
|
22
32
|
|
23
|
-
|
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
|
39
|
+
end
|
40
|
+
```
|
24
41
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
42
|
+
``` ruby
|
43
|
+
User.cached.all # get all users at the first time, after that fetch from cache
|
44
|
+
```
|
45
|
+
|
46
|
+
#### Advanced Options
|
47
|
+
Everything in rails can be cached through **cached**, **cached_for** and remove through **delete_cached**
|
48
|
+
|
49
|
+
##### 1. cached vs delete_cached
|
50
|
+
|
51
|
+
| cached | delete_cached |
|
52
|
+
| -------------------------------------------- | ----------------------------------- |
|
53
|
+
| Anything.cached(opts).function | Anything.delete_cached.function |
|
54
|
+
| Anything.cached(:name, opts).function | Anything.delete_cached :name |
|
55
|
+
| Anything.cached([:name, :id], opts).function | Anything.delete_cached [:name, :id] |
|
56
|
+
| Anything.cached {} | Anything.delete_cached caller_name |
|
57
|
+
| Anything.cached(:name, opts) { ... } | Anything.delete_cached :name |
|
58
|
+
| Anything.cached([:name, :id], opts) { ... } | Anything.delete_cached [:name, :id] |
|
59
|
+
|
60
|
+
|
61
|
+
##### 2. cached_for: is the quite similar to cached, with shortcut for expires_in
|
62
|
+
``` ruby
|
63
|
+
Anything.cached_for(10.hours, opts).function
|
64
|
+
Anything.cached_for(10.hours, opts) {}
|
65
|
+
```
|
66
|
+
|
67
|
+
* ***opts*** is optional, you can ignore it
|
68
|
+
* ***caller_name*** is the function name that line belong to
|
69
|
+
|
70
|
+
##### 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
|
+
|
74
|
+
#### 4. cache support
|
75
|
+
``` ruby
|
76
|
+
Munna.cache.values # => list all key in memcached
|
77
|
+
Munna.cache.clear # => clear all value, key in memcached
|
78
|
+
Munna.cache.get_matched /xxx/ # => get values with satify regex
|
79
|
+
Munna.cache.delete_matched /xxx/ # => delete values with satify regex
|
80
|
+
```
|
data/lib/munna/cache.rb
CHANGED
@@ -5,8 +5,8 @@ module Munna
|
|
5
5
|
Proxy::Write.builder(munna_perform, self, Helper.normalize_params(opts), &block)
|
6
6
|
end
|
7
7
|
|
8
|
-
def cached_for(expires_in, opts
|
9
|
-
|
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
10
|
end
|
11
11
|
|
12
12
|
def delete_cached(*opts)
|
data/lib/munna/perform/base.rb
CHANGED
data/lib/munna/proxy/base.rb
CHANGED
@@ -1,11 +1,6 @@
|
|
1
1
|
module Munna
|
2
2
|
module Proxy
|
3
3
|
class Base
|
4
|
-
def self.builder(kclass, target, options={}, &block)
|
5
|
-
instance = new kclass, target, options, &block
|
6
|
-
block.present? ? instance.__perform__(Execute.new target, block) : instance
|
7
|
-
end
|
8
|
-
|
9
4
|
def initialize(kclass, target, options={}, &block)
|
10
5
|
@kclass = kclass
|
11
6
|
@target = target
|
@@ -21,7 +16,12 @@ module Munna
|
|
21
16
|
end
|
22
17
|
end
|
23
18
|
|
24
|
-
class Write < Base
|
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
26
|
class Delete < Base
|
27
27
|
def self.builder(kclass, target, options={}, &block)
|
data/lib/munna/version.rb
CHANGED
data/lib/munna.rb
CHANGED
@@ -9,15 +9,11 @@ module Munna
|
|
9
9
|
Cache.instance
|
10
10
|
end
|
11
11
|
|
12
|
-
def get_key(
|
13
|
-
if [Symbol, String].include?
|
14
|
-
|
12
|
+
def get_key(params)
|
13
|
+
if [Symbol, String].include? params.class
|
14
|
+
params
|
15
15
|
else
|
16
|
-
|
17
|
-
fragments.each do |fragment|
|
18
|
-
key += '/' + (fragment.is_a?(Array) ? fragment.join('-') : fragment.to_s)
|
19
|
-
end
|
20
|
-
key[1..-1]
|
16
|
+
(params.map {|v| v.is_a?(Array) ? v.join('-') : v}).join('/')
|
21
17
|
end
|
22
18
|
end
|
23
19
|
end
|
data/spec/spec_helper.rb
ADDED
@@ -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
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- |
|
@@ -9,48 +9,48 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-01-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- - ~>
|
18
|
+
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
20
|
version: '1.3'
|
21
21
|
type: :development
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- - ~>
|
25
|
+
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '1.3'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: rake
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
|
-
- -
|
32
|
+
- - ">="
|
33
33
|
- !ruby/object:Gem::Version
|
34
34
|
version: '0'
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- -
|
39
|
+
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '0'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: rails
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
|
-
- -
|
46
|
+
- - ">="
|
47
47
|
- !ruby/object:Gem::Version
|
48
48
|
version: '0'
|
49
49
|
type: :runtime
|
50
50
|
prerelease: false
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
|
-
- -
|
53
|
+
- - ">="
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '0'
|
56
56
|
description: Optimize rails's expensive action with cache through Cache
|
@@ -60,7 +60,8 @@ executables: []
|
|
60
60
|
extensions: []
|
61
61
|
extra_rdoc_files: []
|
62
62
|
files:
|
63
|
-
- .gitignore
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
64
65
|
- Gemfile
|
65
66
|
- LICENSE.txt
|
66
67
|
- README.md
|
@@ -80,6 +81,7 @@ files:
|
|
80
81
|
- lib/munna/version.rb
|
81
82
|
- lib/rake/munna.rake
|
82
83
|
- munna.gemspec
|
84
|
+
- spec/spec_helper.rb
|
83
85
|
homepage: ''
|
84
86
|
licenses:
|
85
87
|
- MIT
|
@@ -90,18 +92,19 @@ require_paths:
|
|
90
92
|
- lib
|
91
93
|
required_ruby_version: !ruby/object:Gem::Requirement
|
92
94
|
requirements:
|
93
|
-
- -
|
95
|
+
- - ">="
|
94
96
|
- !ruby/object:Gem::Version
|
95
97
|
version: '0'
|
96
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
99
|
requirements:
|
98
|
-
- -
|
100
|
+
- - ">="
|
99
101
|
- !ruby/object:Gem::Version
|
100
102
|
version: '0'
|
101
103
|
requirements: []
|
102
104
|
rubyforge_project:
|
103
|
-
rubygems_version: 2.1
|
105
|
+
rubygems_version: 2.2.1
|
104
106
|
signing_key:
|
105
107
|
specification_version: 4
|
106
108
|
summary: Add cache for your object
|
107
|
-
test_files:
|
109
|
+
test_files:
|
110
|
+
- spec/spec_helper.rb
|