munna 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +22 -0
- data/LICENSE.txt +23 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/munna/cache.rb +67 -0
- data/lib/munna/extensions/activerecord_method.rb +14 -0
- data/lib/munna/extensions/basic_method.rb +17 -0
- data/lib/munna/extensions/helper.rb +18 -0
- data/lib/munna/extensions/object_method.rb +18 -0
- data/lib/munna/perform/active_record.rb +35 -0
- data/lib/munna/perform/base.rb +34 -0
- data/lib/munna/perform/object.rb +9 -0
- data/lib/munna/proxy/base.rb +33 -0
- data/lib/munna/proxy/execute.rb +22 -0
- data/lib/munna/railtie.rb +9 -0
- data/lib/munna/version.rb +3 -0
- data/lib/munna.rb +44 -0
- data/lib/rake/munna.rake +10 -0
- data/munna.gemspec +24 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 081e73bfd8f97bc0892e612f1aaa2c4181d60b58
|
4
|
+
data.tar.gz: ce07091638d9e7c174cdf9a47f0a2755597fe20c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8c41b1cef5c5e2614ba15876a86875d716a7087854549b3bf347fe0539c9c8f06e89a8498787399924681fb737bd22759e6ede62595866e34689c1b3f0e93c31
|
7
|
+
data.tar.gz: aab933212c79a8991626d031f06d1aa2d447640c53c6df249a31e3e0d45ed84bc8af88bc5256f46dcd7b6e911f38ad0dc27a105813b32593ce436920f1261e5a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
gem 'rails'
|
6
|
+
|
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
|
13
|
+
end
|
14
|
+
|
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'
|
22
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2013 MQuy
|
2
|
+
|
3
|
+
|
4
|
+
MIT License
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
a copy of this software and associated documentation files (the
|
8
|
+
"Software"), to deal in the Software without restriction, including
|
9
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
the following conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be
|
15
|
+
included in all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Munna
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'munna'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install munna
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
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
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/munna/cache.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module Munna
|
4
|
+
class Cache
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
def initalize
|
8
|
+
values
|
9
|
+
end
|
10
|
+
|
11
|
+
def values
|
12
|
+
Rails.cache.fetch(Munna::KEY) { Hash.new }
|
13
|
+
end
|
14
|
+
|
15
|
+
def clear
|
16
|
+
values.keys.each {|k| Rails.cache.delete k }
|
17
|
+
Rails.cache.delete(Munna::KEY)
|
18
|
+
end
|
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
|
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
|
37
|
+
|
38
|
+
def delete_matched(matches)
|
39
|
+
matches.each {|m| Munna.cache.delete(m)}
|
40
|
+
end
|
41
|
+
|
42
|
+
def get_matched(matches)
|
43
|
+
matches
|
44
|
+
end
|
45
|
+
|
46
|
+
def handle_key(action, key)
|
47
|
+
Rails.cache.write(Munna::KEY, send("#{action}_key", Munna.cache.values, key))
|
48
|
+
end
|
49
|
+
|
50
|
+
def write_key(values, key)
|
51
|
+
values[key] = values[key].to_i + 1 and values
|
52
|
+
end
|
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
|
61
|
+
|
62
|
+
def normailize_value(key)
|
63
|
+
key.gsub /-[^\/]+/, ''
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Munna
|
2
|
+
module Extensions
|
3
|
+
module ActiveRecord
|
4
|
+
include Basic
|
5
|
+
|
6
|
+
def munna_perform
|
7
|
+
Perform::ActiveRecord
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveRecord::Base.send(:include, Munna::Extensions::ActiveRecord)
|
14
|
+
ActiveRecord::Base.send(:extend, Munna::Extensions::ActiveRecord)
|
@@ -0,0 +1,17 @@
|
|
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
|
7
|
+
|
8
|
+
def cached_for(expires_in, opts={}, &block)
|
9
|
+
cached(opts.merge(:expires_in => expires_in), &block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def delete_cached(*opts)
|
13
|
+
Proxy::Delete.builder(munna_perform, self, Helper.normalize_params(opts))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
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
|
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
|
13
|
+
|
14
|
+
cache_opts
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Munna
|
2
|
+
module Extensions
|
3
|
+
module Object
|
4
|
+
include Basic
|
5
|
+
|
6
|
+
def cached_key
|
7
|
+
Munna.get_key [self.class, object_id]
|
8
|
+
end
|
9
|
+
|
10
|
+
def munna_perform
|
11
|
+
Perform::Object
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
Object.send(:include, Munna::Extensions::Object)
|
18
|
+
Object.send(:extend, Munna::Extensions::Object)
|
@@ -0,0 +1,35 @@
|
|
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
|
7
|
+
|
8
|
+
def method_name
|
9
|
+
[super, (send(@execute.name) rescue nil)].compact
|
10
|
+
end
|
11
|
+
|
12
|
+
def normalize(values)
|
13
|
+
values.is_a?(::ActiveRecord::Relation) ? values.to_a : values
|
14
|
+
end
|
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
|
24
|
+
|
25
|
+
class ActiveRecordMethod
|
26
|
+
def initialize(target)
|
27
|
+
@target = target
|
28
|
+
end
|
29
|
+
|
30
|
+
def all
|
31
|
+
@target.maximum(:updated_at).to_i
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,34 @@
|
|
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
|
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
|
16
|
+
|
17
|
+
def perform_delete
|
18
|
+
Munna.cache.delete cached_key
|
19
|
+
end
|
20
|
+
|
21
|
+
def cached_key
|
22
|
+
@cached_key ||= Munna.get_key [target_name, method_name]
|
23
|
+
end
|
24
|
+
|
25
|
+
def method_name
|
26
|
+
@opts[:key] || @execute.name || @opts[:caller_name]
|
27
|
+
end
|
28
|
+
|
29
|
+
def normalize(value)
|
30
|
+
value
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Munna
|
2
|
+
module Proxy
|
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
|
+
def initialize(kclass, target, options={}, &block)
|
10
|
+
@kclass = kclass
|
11
|
+
@target = target
|
12
|
+
@opts = options
|
13
|
+
end
|
14
|
+
|
15
|
+
def method_missing(name, *args, &block)
|
16
|
+
__perform__ Execute.new @target, {:name => name, :args => args, :block => block}
|
17
|
+
end
|
18
|
+
|
19
|
+
def __perform__(execute)
|
20
|
+
@kclass.new(@target, @opts, execute).send("perform_#{self.class.name.demodulize.downcase}")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Write < Base; end
|
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
|
33
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Munna
|
2
|
+
module Proxy
|
3
|
+
class Execute
|
4
|
+
def initialize(target, object={})
|
5
|
+
@target = target
|
6
|
+
@object = object
|
7
|
+
end
|
8
|
+
|
9
|
+
def name
|
10
|
+
@object.is_a?(Proc) ? nil : @object[:name]
|
11
|
+
end
|
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
|
22
|
+
end
|
data/lib/munna.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'munna/version'
|
2
|
+
require 'munna/railtie' if defined?(Rails::Railtie)
|
3
|
+
require 'munna/cache'
|
4
|
+
|
5
|
+
module Munna
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def cache
|
9
|
+
Cache.instance
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_key(fragments)
|
13
|
+
if [Symbol, String].include? fragments.class
|
14
|
+
fragments.to_s
|
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]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
KEY = 'Munna'
|
26
|
+
|
27
|
+
module Extensions
|
28
|
+
require 'munna/extensions/helper'
|
29
|
+
require 'munna/extensions/basic_method'
|
30
|
+
require 'munna/extensions/object_method'
|
31
|
+
require 'munna/extensions/activerecord_method'
|
32
|
+
end
|
33
|
+
|
34
|
+
module Proxy
|
35
|
+
require 'munna/proxy/base'
|
36
|
+
require 'munna/proxy/execute'
|
37
|
+
end
|
38
|
+
|
39
|
+
module Performs
|
40
|
+
require 'munna/perform/base'
|
41
|
+
require 'munna/perform/object'
|
42
|
+
require 'munna/perform/active_record'
|
43
|
+
end
|
44
|
+
end
|
data/lib/rake/munna.rake
ADDED
data/munna.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'munna/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "munna"
|
8
|
+
spec.version = Munna::VERSION
|
9
|
+
spec.authors = ["MQuy\n"]
|
10
|
+
spec.email = ["sugiacupit@gmail.com"]
|
11
|
+
spec.description = %q{Optimize rails's expensive action with cache through Cache}
|
12
|
+
spec.summary = %q{Add cache for your object}
|
13
|
+
spec.homepage = ""
|
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_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_dependency "rails"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: munna
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- |
|
8
|
+
MQuy
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.3'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.3'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rails
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
description: Optimize rails's expensive action with cache through Cache
|
57
|
+
email:
|
58
|
+
- sugiacupit@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/munna.rb
|
69
|
+
- lib/munna/cache.rb
|
70
|
+
- lib/munna/extensions/activerecord_method.rb
|
71
|
+
- lib/munna/extensions/basic_method.rb
|
72
|
+
- lib/munna/extensions/helper.rb
|
73
|
+
- lib/munna/extensions/object_method.rb
|
74
|
+
- lib/munna/perform/active_record.rb
|
75
|
+
- lib/munna/perform/base.rb
|
76
|
+
- lib/munna/perform/object.rb
|
77
|
+
- lib/munna/proxy/base.rb
|
78
|
+
- lib/munna/proxy/execute.rb
|
79
|
+
- lib/munna/railtie.rb
|
80
|
+
- lib/munna/version.rb
|
81
|
+
- lib/rake/munna.rake
|
82
|
+
- munna.gemspec
|
83
|
+
homepage: ''
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.1.3
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: Add cache for your object
|
107
|
+
test_files: []
|