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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c29fdf424ebafc1734aacbc3da69ca1a5ff9563
4
- data.tar.gz: 48cc0cd115be5e997c74763b02569a7c8a978b17
3
+ metadata.gz: 628949fd15aa9d8f74fceddf1a3dd904c9b0fa7b
4
+ data.tar.gz: ee0d9321b713052ba936e1ad4e857749d820cac6
5
5
  SHA512:
6
- metadata.gz: c29896eacf36a4e75f08752e1119ac5f65599894191ce44ee005790782d2164dd28a186df8fd37dc7e729ff4548c2350a1c208a2eca39642ce43ce8381bb9d93
7
- data.tar.gz: 680747417162f543812e2ce6c34bca13b13d5847f6fe8e746cbd0b26c1329f3ba1cbba12e806c701964aa3f1796457e914e7685e99ff446ed27ea33d75781581
6
+ metadata.gz: c10186d4b8dc663a3d1faedcf393b4d211081cbc1e0f94a523fa95990ea1e6c61176d842cbceea7b1ccc3a2914476067e8d20a59dc0d2ba784cecf41a26fcf4a
7
+ data.tar.gz: 7d0b5ccce01c6c6429173b72d7c1a7de7d2d1e07b4326e82e2680da9001810d8f8de3354fc8843ab1945aa615c43b483335b5fc0f087e53319dc5a020f230ffd
@@ -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/cacheable"
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] ||= Cacheable.new
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
- def method_missing(name, *args, &block)
14
- super unless cache.respond_to?(name)
13
+ private
14
+ def method_missing(name, *args, &block)
15
+ super unless cache.respond_to?(name)
15
16
 
16
- define_singleton_method(name) do |*a, &b|
17
- cache.public_send(name, *a, &b)
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
- send(name, *args, &block)
21
- end
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/cacheable/base"
2
- require "restrict_cache/cacheable/active_record_cache"
3
- require "restrict_cache/cacheable/custom_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 Cacheable
6
+ class CacheCollection
7
7
  module CacheKey
8
8
  ACTIVERECORD = :active_record_cache
9
9
  CUSTOM = :custom_cache
10
- ALL = [ACTIVERECORD, CUSTOM]
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
- else
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,14 @@
1
+ module RestrictCache
2
+ class CacheCollection
3
+ class CustomCache < InnerCache
4
+ def add(content)
5
+ @caches[content.class.cache_key] = content
6
+ end
7
+
8
+ def contents(cache_key = nil)
9
+ cache_key ? @caches[cache_key] : nil
10
+ end
11
+ end
12
+ end
13
+ end
14
+
@@ -1,6 +1,6 @@
1
1
  module RestrictCache
2
- class Cacheable
3
- class Base
2
+ class CacheCollection
3
+ class InnerCache
4
4
  def initialize
5
5
  @caches = {}
6
6
  end
@@ -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
+
@@ -0,0 +1,4 @@
1
+ require "restrict_cache/rails_ext/action_controller"
2
+ require "restrict_cache/rails_ext/active_record/base"
3
+ require "restrict_cache/rails_ext/active_record/relation"
4
+
@@ -1,16 +1,18 @@
1
1
  module RestrictCache
2
2
  class Railtie < ::Rails::Railtie
3
3
  initializer "restrict_cache" do
4
- require "restrict_cache/action_controller_ext"
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(:include, RestrictCache::ActionControllerExt)
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(:include, RestrictCache::ActiveRecordExt::Base)
13
- ::ActiveRecord::Relation.send(:include, RestrictCache::ActiveRecordExt::Relation)
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
@@ -1,4 +1,4 @@
1
1
  module RestrictCache
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
4
4
 
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.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-19 00:00:00.000000000 Z
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/action_controller_ext.rb
75
- - lib/restrict_cache/active_record_ext.rb
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/cacheable.rb
78
- - lib/restrict_cache/cacheable/active_record_cache.rb
79
- - lib/restrict_cache/cacheable/base.rb
80
- - lib/restrict_cache/cacheable/custom_cache.rb
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
-
@@ -1,13 +0,0 @@
1
- module RestrictCache
2
- class Cacheable
3
- class CustomCache < Base
4
- def add(cache_key, content)
5
- @caches[cache_key.to_sym] = content
6
- end
7
-
8
- def contents(cache_key = nil)
9
- cache_key ? @caches[cache_key.to_sym] : nil
10
- end
11
- end
12
- end
13
- end