lucid_shopify-cache 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d4bd3b8c8f5fdfbb220f971f45871968a8757ef35c465ea1966dfc28f458d2aa
4
+ data.tar.gz: 4c41f6c64d5c66da2f6aa5a4056748dcff2823dcba1189ba9d9f8fe22ce14622
5
+ SHA512:
6
+ metadata.gz: 7f40200770a8bb8083ebf69a8c78d2619ca30db89934aaee3f70d17459a8126b8c4c1f8cff8a41adb578c87ba53f0c61917cc0867bc2539f02e1f25228e0ac5d
7
+ data.tar.gz: 44e66e48b13e4a3670d1e432fd278a9683235f42fa61e65b6e173bc759feb02d2cb4eda7d1603ad841ceb12ebc84541e93205b299b4affea30707665d1c9f5ae
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ lucid_shopify-cache
2
+ ===================
3
+
4
+ Installation
5
+ ------------
6
+
7
+ Add the following lines to your ‘Gemfile’:
8
+
9
+ git_source :lucid { |r| "https://github.com/lucidnz/gem-lucid_#{r}.git" }
10
+
11
+ gem 'lucid_shopify-cache', lucid: 'shopify-cache'
12
+
13
+
14
+ Usage
15
+ -----
16
+
17
+ ### Make a cached GET request
18
+
19
+ require 'lucid_shopify/cached_get'
20
+
21
+ cached_get = LucidShopify::CachedGet.new
22
+
23
+ args = [request_credentials, 'orders', fields: %w(id tags)]
24
+
25
+ cached_get.(*args)
26
+ cached_get.(*args) # fetched from the cache
27
+
28
+ To clear the cache:
29
+
30
+ cached_get.clear(*args)
31
+
32
+
33
+ ### Example: shop attributes
34
+
35
+ args = [request_credentials, 'shop', {}]
36
+
37
+ cached_get.(*args)['shop']
38
+
39
+ For the next hour, the data will be cached.
40
+
41
+ You might want to set the TTL to a high value and clear it only
42
+ when shop data actually changes. In this case, set up a ‘shop/update’
43
+ webhook which calls:
44
+
45
+ cached_get.clear(*args).(*args)['shop']
46
+
47
+
48
+ ### TTL
49
+
50
+ The default cache TTL is 3600 seconds (one hour), but this can be
51
+ changed by setting the environment variable ‘LUCID_SHOPIFY_CACHE_TTL’.
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LucidShopify
4
+ class Cache
5
+ VERSION = '0.3.1'
6
+ end
7
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module LucidShopify
6
+ class Cache
7
+ TTL = ENV['LUCID_SHOPIFY_CACHE_TTL'] || 3600
8
+
9
+ extend Dry::Initializer
10
+
11
+ # @return [String]
12
+ param :namespace, default: proc { 'lucid_shopify-cache' }
13
+ # @return [Redis]
14
+ option :redis_client, default: proc { defined?(Redis) && Redis.current }
15
+
16
+ #
17
+ # Create a new instance with a new namespace appended to the current one.
18
+ #
19
+ # @param new_namespace [String]
20
+ #
21
+ # @return [Cache]
22
+ #
23
+ # @example
24
+ # cache.add_namespace(myshopify_domain)
25
+ #
26
+ # @example Using the {+} operator alias
27
+ # cache + myshopify_domain
28
+ #
29
+ def add_namespace(new_namespace)
30
+ self.class.new("#{namespace}:#{new_namespace}", redis_client)
31
+ end
32
+
33
+ alias_method :+, :add_namespace
34
+
35
+ #
36
+ # Fetch value from the cache, falling back to the given block when the cache
37
+ # is empty.
38
+ #
39
+ # @param key [String]
40
+ # @param ttl [Integer]
41
+ #
42
+ # @yieldreturn [#to_json]
43
+ #
44
+ # @return [Object]
45
+ #
46
+ def call(key, ttl: TTL)
47
+ key = namespaced_key(key)
48
+
49
+ fetch(key) || cache(key, yield, ttl)
50
+ end
51
+
52
+ #
53
+ # @param key [String]
54
+ #
55
+ # @return [Object, nil]
56
+ #
57
+ private def fetch(key)
58
+ val = redis_client.get(key)
59
+
60
+ val && JSON.parse(val)
61
+ end
62
+
63
+ #
64
+ # @param key [String]
65
+ # @param val [#to_json]
66
+ # @param ttl [Integer]
67
+ #
68
+ # @return [Object]
69
+ #
70
+ private def cache(key, val, ttl)
71
+ redis_client.set(key, val.to_json)
72
+ redis_client.expire(key, ttl)
73
+
74
+ val
75
+ end
76
+
77
+ #
78
+ # @param key [String]
79
+ #
80
+ # @return [String]
81
+ #
82
+ private def namespaced_key(key)
83
+ "#{namespace}:#{key}"
84
+ end
85
+
86
+ #
87
+ # @param key [String]
88
+ #
89
+ def clear(key)
90
+ redis_client.del(namespaced_key(key))
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'uri'
5
+
6
+ require 'lucid_shopify/cache'
7
+
8
+ module LucidShopify
9
+ class CachedGet
10
+ extend Dry::Initializer
11
+
12
+ # @return [Cache]
13
+ option :cache, default: proc { Cache.new }
14
+ # @return [LucidShopify::Client]
15
+ option :client, default: proc { Client.new }
16
+
17
+ #
18
+ # @see {LucidShopify::Client#get}
19
+ #
20
+ # @param ttl [Integer]
21
+ #
22
+ def call(*get_args, ttl: Cache::TTL)
23
+ cache.(key(*get_args), ttl: ttl) { client.get(*get_args) }.freeze
24
+ end
25
+
26
+ #
27
+ # @see {LucidShopify::Client#get}
28
+ #
29
+ # @return [self]
30
+ #
31
+ def clear(*get_args)
32
+ cache.clear(key(*get_args))
33
+
34
+ self
35
+ end
36
+
37
+ #
38
+ # @see {LucidShopify::Client#get}
39
+ #
40
+ private def key(credentials, path, params = {})
41
+ [
42
+ credentials.myshopify_domain,
43
+ path,
44
+ params,
45
+ ].join(':')
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Primarily for Bundler.
4
+
5
+ require 'lucid_shopify/cache'
6
+ require 'lucid_shopify/cached_get'
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lucid_shopify-cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.1
5
+ platform: ruby
6
+ authors:
7
+ - Kelsey Judson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.52.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.52.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-initializer
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.4'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.4'
55
+ description:
56
+ email: kelsey@lucid.nz
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - README.md
62
+ - lib/lucid_shopify-cache.rb
63
+ - lib/lucid_shopify/cache.rb
64
+ - lib/lucid_shopify/cache/version.rb
65
+ - lib/lucid_shopify/cached_get.rb
66
+ homepage: https://github.com/lucidnz/gem-lucid_shopify-cache
67
+ licenses:
68
+ - ISC
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.7.3
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Cache Shopify API data
90
+ test_files: []