foobara-cached-command 0.0.1 → 1.0.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 +4 -4
- data/CHANGELOG.md +10 -0
- data/lib/foobara/cached_command.rb +13 -0
- data/src/foobara/cached_command.rb +25 -8
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0cbe47bcf1bd9b9efd9dff8456fa23647a28dd18b54ecfa1167a40bdbd01c56
|
4
|
+
data.tar.gz: 28da7e0c4682e53fca722d24e1b33fe45ca63b6a089c3c9a594694f36e159f9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1459cf4031df08db4b3dceeb70deed05ff92da58bd876fc9a33725163e98af274c55d72fa740acb63b7b939f9daefc9c9b456a05001216d45218e0ad3bd32ef5
|
7
|
+
data.tar.gz: b00a02a45fd79a4133287b9f14d27b5c5a242a567bdaf2111fd2cc041a91d4f699cd5bde59d54dfaf1e86d3e4525481458313ad0e65bb99bf9bd781f38fabca8
|
data/CHANGELOG.md
CHANGED
@@ -2,3 +2,16 @@ require "foobara/all"
|
|
2
2
|
require "foobara/command_connectors"
|
3
3
|
|
4
4
|
Foobara::Util.require_directory "#{__dir__}/../../src"
|
5
|
+
|
6
|
+
module Foobara
|
7
|
+
module CachedCommand
|
8
|
+
class << self
|
9
|
+
def reset_all
|
10
|
+
Foobara.raise_if_production!("reset_all")
|
11
|
+
|
12
|
+
FileUtils.rm_rf "tmp/cached_command/"
|
13
|
+
Foobara::CachedCommand.cache.clear
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -15,17 +15,22 @@ module Foobara
|
|
15
15
|
unless data
|
16
16
|
if File.exist?(file_path)
|
17
17
|
data = Util.symbolize_keys(JSON.parse(File.read(file_path)))
|
18
|
-
|
19
|
-
|
18
|
+
|
19
|
+
if data.key?(:expires_at)
|
20
|
+
data[:expires_at] = Time.parse(data[:expires_at])
|
21
|
+
cache[key] = data
|
22
|
+
end
|
20
23
|
else
|
21
24
|
return nil
|
22
25
|
end
|
23
26
|
end
|
24
27
|
|
25
|
-
if
|
26
|
-
|
27
|
-
|
28
|
-
|
28
|
+
if data.key?(:expires_at)
|
29
|
+
if Time.now > data[:expires_at]
|
30
|
+
cache.delete(key)
|
31
|
+
FileUtils.rm_f(file_path)
|
32
|
+
data = nil
|
33
|
+
end
|
29
34
|
end
|
30
35
|
|
31
36
|
data
|
@@ -40,13 +45,20 @@ module Foobara
|
|
40
45
|
data[:expires_at] = created_at + expiry
|
41
46
|
end
|
42
47
|
|
48
|
+
basedir = File.dirname(file_path)
|
49
|
+
|
50
|
+
FileUtils.mkdir_p(basedir)
|
51
|
+
|
43
52
|
File.write(file_path, JSON.fast_generate(data))
|
44
53
|
cache[key] = data
|
45
54
|
end
|
46
55
|
end
|
47
56
|
|
57
|
+
# We need to prepend this so that we're overriding the method defined in the class we're mixed into
|
48
58
|
module Execute
|
49
59
|
def execute
|
60
|
+
return super if foobara_cache_disabled
|
61
|
+
|
50
62
|
if foobara_cached_value_present?
|
51
63
|
foobara_cached_value
|
52
64
|
else
|
@@ -60,7 +72,8 @@ module Foobara
|
|
60
72
|
include Concern
|
61
73
|
|
62
74
|
inherited_overridable_class_attr_accessor :foobara_cache_expiry,
|
63
|
-
:foobara_cache_serializer
|
75
|
+
:foobara_cache_serializer,
|
76
|
+
:foobara_cache_disabled
|
64
77
|
|
65
78
|
on_include do
|
66
79
|
prepend Execute
|
@@ -83,7 +96,7 @@ module Foobara
|
|
83
96
|
|
84
97
|
# TODO: support various caching strategies like memcached or redis, not just local files
|
85
98
|
def foobara_cache_path
|
86
|
-
"tmp/#{foobara_cache_key}.json"
|
99
|
+
@foobara_cache_path ||= "tmp/cached_command/#{foobara_cache_key}.json".gsub("::", "/")
|
87
100
|
end
|
88
101
|
|
89
102
|
# TODO: support caching based on certain inputs...
|
@@ -121,5 +134,9 @@ module Foobara
|
|
121
134
|
def foobara_cache_expiry
|
122
135
|
self.class.foobara_cache_expiry
|
123
136
|
end
|
137
|
+
|
138
|
+
def foobara_cache_disabled
|
139
|
+
self.class.foobara_cache_disabled
|
140
|
+
end
|
124
141
|
end
|
125
142
|
end
|
metadata
CHANGED
@@ -1,28 +1,28 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: foobara-cached-command
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Miles Georgi
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: foobara
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|
15
15
|
requirements:
|
16
|
-
- - "
|
16
|
+
- - "<"
|
17
17
|
- !ruby/object:Gem::Version
|
18
|
-
version:
|
18
|
+
version: 2.0.0
|
19
19
|
type: :runtime
|
20
20
|
prerelease: false
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
22
22
|
requirements:
|
23
|
-
- - "
|
23
|
+
- - "<"
|
24
24
|
- !ruby/object:Gem::Version
|
25
|
-
version:
|
25
|
+
version: 2.0.0
|
26
26
|
email:
|
27
27
|
- azimux@gmail.com
|
28
28
|
executables: []
|
@@ -57,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
57
|
- !ruby/object:Gem::Version
|
58
58
|
version: '0'
|
59
59
|
requirements: []
|
60
|
-
rubygems_version: 3.6.
|
60
|
+
rubygems_version: 3.6.9
|
61
61
|
specification_version: 4
|
62
62
|
summary: Makes it so that any foobara command will cache its result in memory and
|
63
63
|
on disk.
|