lucid-shopify-cache 0.10.0

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: 4ef20427c5125fb223b136013a38723bee687bff7553b7697170c9e67460c81d
4
+ data.tar.gz: 6881caeceae19fe21dde3118db714872c2f72f445022a23f759151a4512dc293
5
+ SHA512:
6
+ metadata.gz: 6dab0956886319573cf9088b2b1c565be0c08617c8131f6d5a32e1d4bb22f4118242a9f68d175090c56d20a0f03732358cd7322d7fc1d2ad6e10df62a7800520
7
+ data.tar.gz: d3d534a53f07996c4ce7960b49b8af96d622cf53b520d533739c58d0911c2d03bf057d678e496e797d591838c4e8da980368fadb331d2fa90334aa966c09ce17
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ lucid-shopify-cache
2
+ ===================
3
+
4
+ Installation
5
+ ------------
6
+
7
+ Add the gem to your ‘Gemfile’:
8
+
9
+ gem 'lucid-shopify'
10
+ gem 'lucid-shopify-cache'
11
+
12
+
13
+ Usage
14
+ -----
15
+
16
+ ### Make a cached GET request
17
+
18
+ require 'lucid/shopify/cached_get'
19
+
20
+ cached_get = Lucid::Shopify::CachedGet.new
21
+
22
+ args = [request_credentials, 'orders', fields: %w(id tags)]
23
+
24
+ cached_get.(*args)
25
+ cached_get.(*args) # fetched from the cache
26
+
27
+ To clear the cache:
28
+
29
+ cached_get.clear(*args)
30
+
31
+
32
+ ### Example: shop attributes
33
+
34
+ args = [request_credentials, 'shop', {}]
35
+
36
+ cached_get.(*args)['shop']
37
+
38
+ For the next hour, the data will be cached.
39
+
40
+ You might want to set the TTL to a high value and clear it only
41
+ when shop data actually changes. In this case, set up a ‘shop/update’
42
+ webhook which calls:
43
+
44
+ cached_get.clear(*args).(*args)
45
+
46
+
47
+ ### TTL
48
+
49
+ The default cache TTL is 3600 seconds (one hour), but this can be
50
+ changed by setting the environment variable ‘LUCID_SHOPIFY_CACHE_TTL’.
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lucid
4
+ module Shopify
5
+ class Cache
6
+ VERSION = '0.10.0'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'redis'
4
+ require 'cbor' # less parsing overhead, and smaller than JSON
5
+
6
+ module Lucid
7
+ module Shopify
8
+ class Cache
9
+ TTL = ENV['LUCID_SHOPIFY_CACHE_TTL'] || 3600
10
+
11
+ extend Dry::Initializer
12
+
13
+ # @return [String]
14
+ param :namespace, default: proc { 'lucid/shopify-cache' }
15
+ # @return [Redis]
16
+ option :redis_client, default: proc { Redis.current }
17
+
18
+ # Create a new instance with a new namespace appended to the current one.
19
+ #
20
+ # @param new_namespace [String]
21
+ #
22
+ # @return [Cache]
23
+ #
24
+ # @example
25
+ # cache.add_namespace(myshopify_domain)
26
+ #
27
+ # @example Using the {+} operator alias
28
+ # cache + myshopify_domain
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
+ # Fetch value from the cache, falling back to the given block when the cache
36
+ # is empty.
37
+ #
38
+ # @param key [String]
39
+ # @param ttl [Integer]
40
+ #
41
+ # @yieldreturn [#to_cbor]
42
+ #
43
+ # @return [Object]
44
+ def call(key, ttl: TTL)
45
+ key = namespaced_key(key)
46
+
47
+ fetch(key) || cache(key, yield, ttl)
48
+ end
49
+
50
+ # @param key [String]
51
+ #
52
+ # @return [Object, nil]
53
+ private def fetch(key)
54
+ val = redis_client.get(key)
55
+
56
+ val && CBOR.decode(val)
57
+ end
58
+
59
+ # @param key [String]
60
+ # @param val [#to_cbor]
61
+ # @param ttl [Integer]
62
+ #
63
+ # @return [Object]
64
+ private def cache(key, val, ttl)
65
+ redis_client.set(key, CBOR.encode(val))
66
+ redis_client.expire(key, ttl)
67
+
68
+ val
69
+ end
70
+
71
+ # @param key [String]
72
+ #
73
+ # @return [String]
74
+ private def namespaced_key(key)
75
+ "#{namespace}:#{key}"
76
+ end
77
+
78
+ # @param key [String]
79
+ def clear(key)
80
+ redis_client.del(namespaced_key(key))
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'digest'
4
+ require 'net/http'
5
+ require 'uri'
6
+
7
+ require 'lucid/shopify/cache'
8
+
9
+ module Lucid
10
+ module Shopify
11
+ class CachedGet
12
+ extend Dry::Initializer
13
+
14
+ # @return [Cache]
15
+ option :cache, default: proc { Cache.new }
16
+ # @return [Lucid::Shopify::Client]
17
+ option :client, default: proc { Client.new }
18
+
19
+ # @see {Lucid::Shopify::Client#get}
20
+ #
21
+ # @param ttl [Integer]
22
+ def call(*get_args, ttl: Cache::TTL)
23
+ cache.(key(*get_args), ttl: ttl) { client.get(*get_args).to_h }.freeze
24
+ end
25
+
26
+ # @see {Lucid::Shopify::Client#get}
27
+ #
28
+ # @return [self]
29
+ def clear(*get_args)
30
+ cache.clear(key(*get_args))
31
+
32
+ self
33
+ end
34
+
35
+ # @see {Lucid::Shopify::Client#get}
36
+ private def key(credentials, path, params = {})
37
+ Digest::MD5.hexdigest([
38
+ credentials.myshopify_domain,
39
+ path,
40
+ params,
41
+ ].join("\x1f")) # ASCII unit separator
42
+ end
43
+ end
44
+ end
45
+ 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,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lucid-shopify-cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.10.0
5
+ platform: ruby
6
+ authors:
7
+ - Kelsey Judson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-10-13 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.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.8'
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.67'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: '0.67'
41
+ - !ruby/object:Gem::Dependency
42
+ name: cbor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.5'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: dry-initializer
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: lucid-shopify
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.34'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.34'
83
+ - !ruby/object:Gem::Dependency
84
+ name: redis
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.1'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.1'
97
+ description:
98
+ email: kelsey@lucid.nz
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - README.md
104
+ - lib/lucid-shopify-cache.rb
105
+ - lib/lucid/shopify/cache.rb
106
+ - lib/lucid/shopify/cache/version.rb
107
+ - lib/lucid/shopify/cached_get.rb
108
+ homepage: https://github.com/lucidnz/gem-lucid-shopify-cache
109
+ licenses:
110
+ - ISC
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubygems_version: 3.0.3
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Cache Shopify API data
131
+ test_files: []