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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 081e73bfd8f97bc0892e612f1aaa2c4181d60b58
4
- data.tar.gz: ce07091638d9e7c174cdf9a47f0a2755597fe20c
3
+ metadata.gz: 18d02948e6426b1492c72e4856414458efbdc506
4
+ data.tar.gz: 10ffc7cb7b37cac78ec12af8d841b746fbe0adc2
5
5
  SHA512:
6
- metadata.gz: 8c41b1cef5c5e2614ba15876a86875d716a7087854549b3bf347fe0539c9c8f06e89a8498787399924681fb737bd22759e6ede62595866e34689c1b3f0e93c31
7
- data.tar.gz: aab933212c79a8991626d031f06d1aa2d447640c53c6df249a31e3e0d45ed84bc8af88bc5256f46dcd7b6e911f38ad0dc27a105813b32593ce436920f1261e5a
6
+ metadata.gz: 7b2d9b24b2f4512e638b0786c53a9777aeb9c2771a7194cf2daa0cc150ecab1e5b6b9f2b9d2ce9e0503bbebf05e369d89a536b4e425e97a1c71271ea9918df3c
7
+ data.tar.gz: fc087b37ee4d7220e9b3471e96fdd4b1728f0485487a7c1225f5f1c5027a6f70547f895a41a75658d5bff8b48549078460e3d386dd249866c5313fbf57a77e24
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/README.md CHANGED
@@ -1,29 +1,80 @@
1
1
  # Munna
2
2
 
3
- TODO: Write a gem description
3
+ Simple cache wrapper for rails ActiveRecord, Object, ... to speed up your site
4
4
 
5
- ## Installation
5
+ ### Installation
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
- gem 'munna'
9
+ gem 'munna'
10
10
 
11
- And then execute:
11
+ ### Usage
12
12
 
13
- $ bundle
13
+ #### ActiveRecord
14
14
 
15
- Or install it yourself as:
15
+ ``` ruby
16
+ class Profile < ActiveRecord::Base
17
+ after_destroy :clear_cache
16
18
 
17
- $ gem install munna
19
+ def clear_cache
20
+ User.delete_cached :list_with_profiles
21
+ end
22
+ end
18
23
 
19
- ## Usage
24
+ class User < ActiveRecord::Base
25
+ has_many :profiles
20
26
 
21
- TODO: Write usage instructions here
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
- ## Contributing
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
- 1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
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
@@ -4,7 +4,7 @@ module Munna
4
4
  class Cache
5
5
  include Singleton
6
6
 
7
- def initalize
7
+ def initialize
8
8
  values
9
9
  end
10
10
 
@@ -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={}, &block)
9
- cached(opts.merge(:expires_in => expires_in), &block)
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)
@@ -9,8 +9,8 @@ module Munna
9
9
 
10
10
  def perform_write
11
11
  Munna.cache.write cached_key
12
- Rails.cache.fetch(cached_key, @opts) do
13
- normalize(@execute.value)
12
+ Rails.cache.fetch cached_key, @opts do
13
+ normalize @execute.value
14
14
  end
15
15
  end
16
16
 
@@ -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; 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
26
  class Delete < Base
27
27
  def self.builder(kclass, target, options={}, &block)
data/lib/munna/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Munna
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/munna.rb CHANGED
@@ -9,15 +9,11 @@ module Munna
9
9
  Cache.instance
10
10
  end
11
11
 
12
- def get_key(fragments)
13
- if [Symbol, String].include? fragments.class
14
- fragments.to_s
12
+ def get_key(params)
13
+ if [Symbol, String].include? params.class
14
+ params
15
15
  else
16
- key = ''
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
@@ -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.1
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: 2013-10-25 00:00:00.000000000 Z
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.3
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