restrict_cache 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/restrict_cache.rb +2 -1
- data/lib/restrict_cache/accessible.rb +12 -7
- data/lib/restrict_cache/base.rb +31 -0
- data/lib/restrict_cache/{cacheable.rb → cache_collection.rb} +11 -6
- data/lib/restrict_cache/cache_collection/active_record_cache.rb +25 -0
- data/lib/restrict_cache/cache_collection/custom_cache.rb +14 -0
- data/lib/restrict_cache/{cacheable/base.rb → cache_collection/inner_cache.rb} +2 -2
- data/lib/restrict_cache/rails_ext/action_controller.rb +23 -0
- data/lib/restrict_cache/rails_ext/active_record/base.rb +20 -0
- data/lib/restrict_cache/rails_ext/active_record/relation.rb +54 -0
- data/lib/restrict_cache/rails_ext/all.rb +4 -0
- data/lib/restrict_cache/railtie.rb +7 -5
- data/lib/restrict_cache/version.rb +1 -1
- metadata +11 -8
- data/lib/restrict_cache/action_controller_ext.rb +0 -21
- data/lib/restrict_cache/active_record_ext.rb +0 -64
- data/lib/restrict_cache/cacheable/active_record_cache.rb +0 -20
- data/lib/restrict_cache/cacheable/custom_cache.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 628949fd15aa9d8f74fceddf1a3dd904c9b0fa7b
|
4
|
+
data.tar.gz: ee0d9321b713052ba936e1ad4e857749d820cac6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c10186d4b8dc663a3d1faedcf393b4d211081cbc1e0f94a523fa95990ea1e6c61176d842cbceea7b1ccc3a2914476067e8d20a59dc0d2ba784cecf41a26fcf4a
|
7
|
+
data.tar.gz: 7d0b5ccce01c6c6429173b72d7c1a7de7d2d1e07b4326e82e2680da9001810d8f8de3354fc8843ab1945aa615c43b483335b5fc0f087e53319dc5a020f230ffd
|
data/lib/restrict_cache.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "restrict_cache/version"
|
2
|
+
require "restrict_cache/base"
|
2
3
|
require "restrict_cache/accessible"
|
3
|
-
require "restrict_cache/
|
4
|
+
require "restrict_cache/cache_collection"
|
4
5
|
require "restrict_cache/cache_sweeper"
|
5
6
|
require "restrict_cache/railtie" if defined?(Rails)
|
6
7
|
|
@@ -3,22 +3,27 @@ module RestrictCache
|
|
3
3
|
THREAD_KEY = :restrict_cache
|
4
4
|
|
5
5
|
def cache
|
6
|
-
Thread.current[THREAD_KEY] ||=
|
6
|
+
Thread.current[THREAD_KEY] ||= CacheCollection.new
|
7
7
|
end
|
8
8
|
|
9
9
|
def clear
|
10
10
|
Thread.current[THREAD_KEY] = nil
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
|
-
|
13
|
+
private
|
14
|
+
def method_missing(name, *args, &block)
|
15
|
+
super unless cache.respond_to?(name)
|
15
16
|
|
16
|
-
|
17
|
-
|
17
|
+
define_singleton_method(name) do |*a, &b|
|
18
|
+
cache.public_send(name, *a, &b)
|
19
|
+
end
|
20
|
+
|
21
|
+
send(name, *args, &block)
|
18
22
|
end
|
19
23
|
|
20
|
-
|
21
|
-
|
24
|
+
def respond_to_missing?(name, include_private = false)
|
25
|
+
cache.respond_to?(name)
|
26
|
+
end
|
22
27
|
end
|
23
28
|
|
24
29
|
extend Accessible
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module RestrictCache
|
2
|
+
class Base
|
3
|
+
class << self
|
4
|
+
def instance
|
5
|
+
_cache = RestrictCache.custom_cache.contents(cache_key)
|
6
|
+
_cache = RestrictCache.custom_cache.add(self.new) unless _cache
|
7
|
+
_cache
|
8
|
+
end
|
9
|
+
|
10
|
+
def cache_key
|
11
|
+
self.name
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
def method_missing(name, *args, &block)
|
16
|
+
super unless instance.respond_to?(name)
|
17
|
+
|
18
|
+
define_singleton_method(name) do |*a, &b|
|
19
|
+
instance.public_send(name, *a, &b)
|
20
|
+
end
|
21
|
+
|
22
|
+
send(name, *args, &block)
|
23
|
+
end
|
24
|
+
|
25
|
+
def respond_to_missing?(name, include_private = false)
|
26
|
+
instance.respond_to?(name)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
@@ -1,20 +1,25 @@
|
|
1
|
-
require "restrict_cache/
|
2
|
-
require "restrict_cache/
|
3
|
-
require "restrict_cache/
|
1
|
+
require "restrict_cache/cache_collection/inner_cache"
|
2
|
+
require "restrict_cache/cache_collection/active_record_cache" if defined?(ActiveRecord)
|
3
|
+
require "restrict_cache/cache_collection/custom_cache"
|
4
4
|
|
5
5
|
module RestrictCache
|
6
|
-
class
|
6
|
+
class CacheCollection
|
7
7
|
module CacheKey
|
8
8
|
ACTIVERECORD = :active_record_cache
|
9
9
|
CUSTOM = :custom_cache
|
10
|
-
ALL = [
|
10
|
+
ALL = [
|
11
|
+
defined?(ActiveRecord) ? ACTIVERECORD : nil,
|
12
|
+
CUSTOM,
|
13
|
+
].compact.freeze
|
11
14
|
|
12
15
|
def self.get(content)
|
13
16
|
case
|
14
17
|
when defined?(ActiveRecord) && content.class < ActiveRecord::Base
|
15
18
|
ACTIVERECORD
|
16
|
-
|
19
|
+
when content.class < RestrictCache::Base
|
17
20
|
CUSTOM
|
21
|
+
else
|
22
|
+
raise "unknown cache class"
|
18
23
|
end
|
19
24
|
end
|
20
25
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module RestrictCache
|
2
|
+
class CacheCollection
|
3
|
+
class ActiveRecordCache < InnerCache
|
4
|
+
def add(content)
|
5
|
+
tbl_name, index = table_name_of(content), index_of(content)
|
6
|
+
@caches[tbl_name] ||= {}
|
7
|
+
@caches[tbl_name][index] = content
|
8
|
+
end
|
9
|
+
|
10
|
+
def contents(_table_name)
|
11
|
+
@caches[_table_name.to_sym]
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
def table_name_of(content)
|
16
|
+
content.class.table_name.to_sym
|
17
|
+
end
|
18
|
+
|
19
|
+
def index_of(content)
|
20
|
+
content.public_send content.class.primary_key
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module RestrictCache
|
2
|
+
module RailsExt
|
3
|
+
module ActionController
|
4
|
+
include ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def cache_sweep_action(*args)
|
8
|
+
options = args.extract_options!
|
9
|
+
actions = args.empty? ? {} : {only: args}
|
10
|
+
options.merge! actions unless actions.empty?
|
11
|
+
around_action :sweep_restrict_cache, options
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def sweep_restrict_cache
|
16
|
+
yield
|
17
|
+
ensure
|
18
|
+
RestrictCache.clear
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module RestrictCache
|
2
|
+
module RailsExt
|
3
|
+
module ActiveRecord
|
4
|
+
module Base
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
%i(find_with_restrict_cache find_with_rc).each do |key|
|
9
|
+
delegate key, to: :all
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def restrict_cache
|
14
|
+
RestrictCache.add(self)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module RestrictCache
|
2
|
+
module RailsExt
|
3
|
+
module ActiveRecord
|
4
|
+
module Relation
|
5
|
+
def find_and_restrict_cache(*args)
|
6
|
+
records = find(*args)
|
7
|
+
Array(records).each(&:restrict_cache)
|
8
|
+
records
|
9
|
+
end
|
10
|
+
|
11
|
+
def find_from_restrict_cache(*args)
|
12
|
+
return nil unless cached_contents
|
13
|
+
|
14
|
+
case args.first
|
15
|
+
when Integer
|
16
|
+
cached_contents[args.first]
|
17
|
+
when String
|
18
|
+
cached_contents[args.first.to_i]
|
19
|
+
when Array
|
20
|
+
args.first.map {|index| cached_contents[index.to_i] }
|
21
|
+
else
|
22
|
+
raise "unknown argument: #{args.inspect}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def find_with_restrict_cache(*args, &block)
|
27
|
+
if restrict_cached?(*args)
|
28
|
+
records = find_from_restrict_cache(*args)
|
29
|
+
else
|
30
|
+
records = find_and_restrict_cache(*args)
|
31
|
+
end
|
32
|
+
|
33
|
+
block_given? ? records.each {|record| block.call(record) } : records
|
34
|
+
end
|
35
|
+
alias_method :find_with_rc, :find_with_restrict_cache
|
36
|
+
|
37
|
+
def with_restrict_cache
|
38
|
+
self.each(&:restrict_cache) && self
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def cached_contents
|
43
|
+
RestrictCache.send(CacheCollection::CacheKey::ACTIVERECORD).contents(self.table_name)
|
44
|
+
end
|
45
|
+
|
46
|
+
def restrict_cached?(*args)
|
47
|
+
return false unless cached_contents
|
48
|
+
args.all? {|index| cached_contents.include?(index) }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
@@ -1,16 +1,18 @@
|
|
1
1
|
module RestrictCache
|
2
2
|
class Railtie < ::Rails::Railtie
|
3
3
|
initializer "restrict_cache" do
|
4
|
-
require "restrict_cache/
|
5
|
-
require "restrict_cache/active_record_ext"
|
4
|
+
require "restrict_cache/rails_ext/all"
|
6
5
|
|
7
6
|
ActiveSupport.on_load(:action_controller) do
|
8
|
-
::ActionController::Base.send(
|
7
|
+
::ActionController::Base.send(
|
8
|
+
:include, RestrictCache::RailsExt::ActionController)
|
9
9
|
end
|
10
10
|
|
11
11
|
ActiveSupport.on_load(:active_record) do
|
12
|
-
::ActiveRecord::Base.send(
|
13
|
-
|
12
|
+
::ActiveRecord::Base.send(
|
13
|
+
:include, RestrictCache::RailsExt::ActiveRecord::Base)
|
14
|
+
::ActiveRecord::Relation.send(
|
15
|
+
:include, RestrictCache::RailsExt::ActiveRecord::Relation)
|
14
16
|
end
|
15
17
|
end
|
16
18
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restrict_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- i2bskn
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,13 +71,16 @@ files:
|
|
71
71
|
- bin/setup
|
72
72
|
- lib/restrict_cache.rb
|
73
73
|
- lib/restrict_cache/accessible.rb
|
74
|
-
- lib/restrict_cache/
|
75
|
-
- lib/restrict_cache/
|
74
|
+
- lib/restrict_cache/base.rb
|
75
|
+
- lib/restrict_cache/cache_collection.rb
|
76
|
+
- lib/restrict_cache/cache_collection/active_record_cache.rb
|
77
|
+
- lib/restrict_cache/cache_collection/custom_cache.rb
|
78
|
+
- lib/restrict_cache/cache_collection/inner_cache.rb
|
76
79
|
- lib/restrict_cache/cache_sweeper.rb
|
77
|
-
- lib/restrict_cache/
|
78
|
-
- lib/restrict_cache/
|
79
|
-
- lib/restrict_cache/
|
80
|
-
- lib/restrict_cache/
|
80
|
+
- lib/restrict_cache/rails_ext/action_controller.rb
|
81
|
+
- lib/restrict_cache/rails_ext/active_record/base.rb
|
82
|
+
- lib/restrict_cache/rails_ext/active_record/relation.rb
|
83
|
+
- lib/restrict_cache/rails_ext/all.rb
|
81
84
|
- lib/restrict_cache/railtie.rb
|
82
85
|
- lib/restrict_cache/version.rb
|
83
86
|
- restrict_cache.gemspec
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module RestrictCache
|
2
|
-
module ActionControllerExt
|
3
|
-
include ActiveSupport::Concern
|
4
|
-
|
5
|
-
module ClassMethods
|
6
|
-
def cache_sweep_action(*args)
|
7
|
-
options = args.extract_options!
|
8
|
-
actions = args.empty? ? {} : {only: args}
|
9
|
-
options.merge! actions unless actions.empty?
|
10
|
-
around_action :sweep_restrict_cache, options
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def sweep_restrict_cache
|
15
|
-
yield
|
16
|
-
ensure
|
17
|
-
RestrictCache.clear
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
@@ -1,64 +0,0 @@
|
|
1
|
-
module RestrictCache
|
2
|
-
module ActiveRecordExt
|
3
|
-
module Base
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
module ClassMethods
|
7
|
-
delegate :find_with_restrict_cache, to: :all
|
8
|
-
end
|
9
|
-
|
10
|
-
def restrict_cache
|
11
|
-
RestrictCache.add(self)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
module Relation
|
16
|
-
AR_CACHE_KEY = Cacheable::CacheKey::ACTIVERECORD
|
17
|
-
|
18
|
-
def find_and_restrict_cache(arg)
|
19
|
-
records = find(arg)
|
20
|
-
Array(records).each(&:restrict_cache)
|
21
|
-
records
|
22
|
-
end
|
23
|
-
|
24
|
-
def find_from_restrict_cache(arg)
|
25
|
-
contents = RestrictCache.send(AR_CACHE_KEY).contents(self.table_name)
|
26
|
-
return nil unless contents
|
27
|
-
|
28
|
-
case arg
|
29
|
-
when Integer
|
30
|
-
contents[arg]
|
31
|
-
when String
|
32
|
-
contents[arg.to_i]
|
33
|
-
when Array
|
34
|
-
args.map {|index| contents[index.to_i] }
|
35
|
-
else
|
36
|
-
raise "unknown argument: #{arg.inspect}"
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def find_with_restrict_cache(arg)
|
41
|
-
if restrict_cached?(Array(arg))
|
42
|
-
records = Array(arg).map {|index| find_from_restrict_cache(index) }
|
43
|
-
records.size > 1 ? records : records.first
|
44
|
-
else
|
45
|
-
find_and_restrict_cache(arg)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def with_restrict_cache
|
50
|
-
self.each(&:restrict_cache)
|
51
|
-
self
|
52
|
-
end
|
53
|
-
|
54
|
-
private
|
55
|
-
def restrict_cached?(args)
|
56
|
-
content = RestrictCache.send(AR_CACHE_KEY).contents(self.table_name)
|
57
|
-
return false unless content
|
58
|
-
ids = content.keys
|
59
|
-
args.all? {|index| ids.include?(index) }
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
@@ -1,20 +0,0 @@
|
|
1
|
-
module RestrictCache
|
2
|
-
class Cacheable
|
3
|
-
class ActiveRecordCache < Base
|
4
|
-
def add(content)
|
5
|
-
@caches[table_name(content)] ||= {}
|
6
|
-
@caches[table_name(content)][content.id] = content
|
7
|
-
end
|
8
|
-
|
9
|
-
def contents(_table_name)
|
10
|
-
@caches[_table_name.to_sym]
|
11
|
-
end
|
12
|
-
|
13
|
-
private
|
14
|
-
def table_name(content)
|
15
|
-
content.class.table_name.to_sym
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|