cachengue 0.1.1 → 0.2.0
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 +5 -5
- data/.rubocop.yml +2 -9
- data/Gemfile.lock +6 -6
- data/README.md +1 -1
- data/cachengue.gemspec +2 -1
- data/lib/cachengue.rb +6 -6
- data/lib/cachengue/caching.rb +54 -24
- data/lib/cachengue/namespace.rb +56 -0
- data/lib/cachengue/proxy.rb +3 -2
- data/lib/cachengue/version.rb +1 -1
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1d61f4fc468d164194855289a59933c239687ba325f33470816ab45dbf383a50
|
4
|
+
data.tar.gz: 68fc8ff64e399aca19acc01fbf20272537a67a86c08722967cf7de2e7f19ba0a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: edda0834cac796434c6f0e9df1137323f7cd6c1a5ab7713a5ef8fdb251caa8d04682f132ea22ff06a18058d5ec52a8370b6b5b00afb0cb533f4b112a810af2db
|
7
|
+
data.tar.gz: ea1e368ed2bc7897c66ec6cbf9a20e82489750d41aea1ee4b1d3bf4fef1779009ffe080d69589e40ec3fe7b85616d48ec410cd1c0039258c01ad112fa1e25f06
|
data/.rubocop.yml
CHANGED
@@ -1,17 +1,12 @@
|
|
1
1
|
AllCops:
|
2
2
|
Exclude:
|
3
3
|
- bin/*
|
4
|
-
- config/**/*
|
5
|
-
- db/*
|
6
|
-
- db/migrate/*
|
7
|
-
- lib/tasks/*
|
8
4
|
- test/*
|
5
|
+
- cachengue.gemspec
|
9
6
|
- Gemfile
|
10
7
|
- Rakefile
|
11
|
-
- config.ru
|
12
|
-
- lib/onfleet/onfleet-ruby/**/*
|
13
8
|
|
14
|
-
TargetRubyVersion: 2.
|
9
|
+
TargetRubyVersion: 2.5
|
15
10
|
|
16
11
|
Layout/IndentHash:
|
17
12
|
EnforcedStyle: consistent
|
@@ -63,5 +58,3 @@ Style/Documentation:
|
|
63
58
|
|
64
59
|
Style/Lambda:
|
65
60
|
Enabled: false
|
66
|
-
|
67
|
-
|
data/Gemfile.lock
CHANGED
@@ -1,18 +1,19 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
cachengue (0.
|
4
|
+
cachengue (0.2.0)
|
5
|
+
activesupport (>= 5.1)
|
5
6
|
|
6
7
|
GEM
|
7
8
|
remote: https://rubygems.org/
|
8
9
|
specs:
|
9
|
-
activesupport (5.1
|
10
|
+
activesupport (5.2.1)
|
10
11
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
11
12
|
i18n (>= 0.7, < 2)
|
12
13
|
minitest (~> 5.1)
|
13
14
|
tzinfo (~> 1.1)
|
14
15
|
concurrent-ruby (1.0.5)
|
15
|
-
i18n (1.
|
16
|
+
i18n (1.1.1)
|
16
17
|
concurrent-ruby (~> 1.0)
|
17
18
|
minitest (5.3.5)
|
18
19
|
rake (10.5.0)
|
@@ -24,11 +25,10 @@ PLATFORMS
|
|
24
25
|
ruby
|
25
26
|
|
26
27
|
DEPENDENCIES
|
27
|
-
activesupport (~> 5.1.5)
|
28
28
|
bundler (~> 1.16)
|
29
29
|
cachengue!
|
30
|
-
minitest (~> 5.3
|
30
|
+
minitest (~> 5.3)
|
31
31
|
rake (~> 10.0)
|
32
32
|
|
33
33
|
BUNDLED WITH
|
34
|
-
1.16.
|
34
|
+
1.16.3
|
data/README.md
CHANGED
data/cachengue.gemspec
CHANGED
@@ -36,5 +36,6 @@ Gem::Specification.new do |spec|
|
|
36
36
|
spec.add_development_dependency "bundler", "~> 1.16"
|
37
37
|
spec.add_development_dependency "rake", "~> 10.0"
|
38
38
|
spec.add_development_dependency "minitest", "~> 5.3"
|
39
|
-
|
39
|
+
|
40
|
+
spec.add_runtime_dependency "activesupport", ">= 5.1"
|
40
41
|
end
|
data/lib/cachengue.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'cachengue/version'
|
4
|
-
require 'cachengue/proxy'
|
5
3
|
require 'cachengue/caching'
|
4
|
+
require 'cachengue/namespace'
|
5
|
+
require 'cachengue/proxy'
|
6
|
+
require 'cachengue/version'
|
6
7
|
|
7
8
|
module Cachengue
|
8
9
|
module ActiveRecord
|
9
10
|
extend ActiveSupport::Concern
|
10
11
|
|
11
12
|
included do
|
12
|
-
extend Cachengue::
|
13
|
-
|
14
|
-
|
15
|
-
end
|
13
|
+
extend Cachengue::Namespace
|
14
|
+
|
15
|
+
before_save { self.class.cachengue_clear }
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
data/lib/cachengue/caching.rb
CHANGED
@@ -2,48 +2,80 @@
|
|
2
2
|
|
3
3
|
module Cachengue
|
4
4
|
module Caching
|
5
|
-
|
6
|
-
|
5
|
+
# Reduce memory use and its faster.
|
6
|
+
ALL_KEYS = %w[*].freeze
|
7
|
+
NONE_KEYS = [].freeze
|
7
8
|
|
8
|
-
|
9
|
+
module_function
|
10
|
+
|
11
|
+
def format_key(namespace, method_name, args)
|
12
|
+
args = args.is_a?(Array) ? args.map(&:as_json) : [args.as_json]
|
13
|
+
namespace = key_prefix(namespace)
|
14
|
+
|
15
|
+
"#{namespace}:#{method_name}:#{args}"
|
9
16
|
end
|
10
17
|
|
11
|
-
def
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
18
|
+
def key_prefix(namespace = nil)
|
19
|
+
if namespace.present?
|
20
|
+
"cachengue:#{namespace.to_s.underscore}"
|
21
|
+
else
|
22
|
+
'cachengue'
|
16
23
|
end
|
17
24
|
end
|
18
25
|
|
19
|
-
|
26
|
+
def key_suffix(strategy)
|
27
|
+
strategy == :all ? ALL_KEYS : NONE_KEYS
|
28
|
+
end
|
20
29
|
|
21
|
-
def
|
22
|
-
|
30
|
+
def keys(namespace = nil, strategy = :all)
|
31
|
+
return [] if Rails.env.test?
|
23
32
|
|
24
|
-
|
33
|
+
prefix = key_prefix(namespace)
|
34
|
+
|
35
|
+
key_suffix(strategy).flat_map do |suffix|
|
36
|
+
Rails.cache.data.keys("#{prefix}:#{suffix}")
|
37
|
+
end
|
25
38
|
end
|
26
39
|
|
27
|
-
def
|
28
|
-
|
40
|
+
def clear(namespace = nil, strategy = :all)
|
41
|
+
keys_to_clear = keys(namespace, strategy)
|
42
|
+
|
43
|
+
clear_keys(keys_to_clear)
|
29
44
|
end
|
30
45
|
|
31
|
-
def
|
32
|
-
|
46
|
+
def clear_by(options)
|
47
|
+
key = format_key(
|
48
|
+
options.fetch(:namespace),
|
49
|
+
options[:key] || options.fetch(:method),
|
50
|
+
options.fetch(:args)
|
51
|
+
)
|
33
52
|
|
34
|
-
|
53
|
+
clear_keys([key])
|
35
54
|
end
|
36
55
|
|
37
|
-
def
|
56
|
+
def clear_keys(keys_to_clear)
|
38
57
|
return if Rails.env.test?
|
39
58
|
|
40
|
-
|
59
|
+
keys_to_clear.sum do |k|
|
60
|
+
Rails.cache.delete(k)
|
61
|
+
end
|
41
62
|
end
|
42
63
|
|
43
|
-
def fetch(
|
44
|
-
key =
|
64
|
+
def fetch(namespace, method_name, args, options = {})
|
65
|
+
key = format_key(
|
66
|
+
options[:namespace] || namespace,
|
67
|
+
options[:key] || options[:method] || method_name,
|
68
|
+
options[:args] || args
|
69
|
+
)
|
70
|
+
|
71
|
+
options = options.without(
|
72
|
+
:args,
|
73
|
+
:key,
|
74
|
+
:method,
|
75
|
+
:namespace
|
76
|
+
)
|
45
77
|
|
46
|
-
|
78
|
+
Rails.cache.fetch(key, options) do
|
47
79
|
value = yield(*args)
|
48
80
|
|
49
81
|
if defined?(::ActiveRecord) && value.is_a?(::ActiveRecord::Relation)
|
@@ -52,8 +84,6 @@ module Cachengue
|
|
52
84
|
value
|
53
85
|
end
|
54
86
|
end
|
55
|
-
|
56
|
-
Rails.cache.fetch(key, &new_block)
|
57
87
|
end
|
58
88
|
end
|
59
89
|
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Cachengue
|
4
|
+
module Namespace
|
5
|
+
def cachengue(options = {})
|
6
|
+
if block_given?
|
7
|
+
# Proc.new is the block passed as argument.
|
8
|
+
Caching.fetch(self, nil, [], options, &Proc.new)
|
9
|
+
else
|
10
|
+
Proxy.new(self, options)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def module_cachengue(method_name, options = {})
|
15
|
+
singleton_class.instance_eval do
|
16
|
+
define_method(method_name) do |*args|
|
17
|
+
Caching.fetch(self, method_name, args, options, &Proc.new)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def cachengue_namespace(namespace, strategy: :all)
|
23
|
+
__cachengue_namespaces__[namespace.to_sym] = strategy
|
24
|
+
end
|
25
|
+
|
26
|
+
def cachengue_clear_by(options)
|
27
|
+
Caching.clear_by(
|
28
|
+
options.reverse_merge(namespace: self)
|
29
|
+
)
|
30
|
+
end
|
31
|
+
|
32
|
+
def cachengue_clear
|
33
|
+
__cachengue_namespaces__.inject({}) do |results, (namespace, strategy)|
|
34
|
+
results.merge!(
|
35
|
+
namespace => Caching.clear(namespace, strategy)
|
36
|
+
)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def __cachengue_namespaces__
|
43
|
+
if class_variable_defined?(:@@cachengue_namespaces)
|
44
|
+
class_variable_set(
|
45
|
+
:@@cachengue_namespaces,
|
46
|
+
class_variable_get(:@@cachengue_namespaces)
|
47
|
+
)
|
48
|
+
else
|
49
|
+
class_variable_set(
|
50
|
+
:@@cachengue_namespaces,
|
51
|
+
to_s.to_sym => :all
|
52
|
+
)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/cachengue/proxy.rb
CHANGED
@@ -4,8 +4,9 @@
|
|
4
4
|
|
5
5
|
module Cachengue
|
6
6
|
class Proxy
|
7
|
-
def initialize(classx)
|
7
|
+
def initialize(classx, options = {})
|
8
8
|
@classx = classx
|
9
|
+
@options = options
|
9
10
|
end
|
10
11
|
|
11
12
|
def cache_keys
|
@@ -23,7 +24,7 @@ module Cachengue
|
|
23
24
|
end
|
24
25
|
|
25
26
|
def method_missing(method_name, *args)
|
26
|
-
Cachengue::Caching.fetch(@classx, method_name, args) do
|
27
|
+
Cachengue::Caching.fetch(@classx, method_name, args, @options) do
|
27
28
|
@classx.public_send(method_name, *args)
|
28
29
|
end
|
29
30
|
end
|
data/lib/cachengue/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cachengue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- shipnow developers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -56,14 +56,14 @@ dependencies:
|
|
56
56
|
name: activesupport
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '5.1'
|
62
|
-
type: :
|
62
|
+
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '5.1'
|
69
69
|
description: A gem for caching
|
@@ -84,6 +84,7 @@ files:
|
|
84
84
|
- cachengue.gemspec
|
85
85
|
- lib/cachengue.rb
|
86
86
|
- lib/cachengue/caching.rb
|
87
|
+
- lib/cachengue/namespace.rb
|
87
88
|
- lib/cachengue/proxy.rb
|
88
89
|
- lib/cachengue/version.rb
|
89
90
|
homepage: https://gitlab.com/shipnow/cachengue
|
@@ -106,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
107
|
version: '0'
|
107
108
|
requirements: []
|
108
109
|
rubyforge_project:
|
109
|
-
rubygems_version: 2.6
|
110
|
+
rubygems_version: 2.7.6
|
110
111
|
signing_key:
|
111
112
|
specification_version: 4
|
112
113
|
summary: cache gem for ruby on rails
|