active_cached_resource 0.1.3 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 36d447a38745c6e9f39b708d1de8b800d888b321bf5edae54fe053cc27c96940
4
- data.tar.gz: ab1b634d96e4cf7557b7e55e0e30fba8f008a9c298d50c622a6e6128cc2b88da
3
+ metadata.gz: febe875b913adcca00b5ca5eab9c43b23691912ba4465ce34c3f842ba2c0652c
4
+ data.tar.gz: 39d644336fd27dc4404c8c2d6f130b74e64686c8d923ee6c3b0257fcd1890576
5
5
  SHA512:
6
- metadata.gz: 6803152c873fb3375ab6a320d90a77054e614b009ffc66f5595a976073cb58c222015cab83ca87009223b8769c9a9579c8d70a35568e860739ba38d936a04ab6
7
- data.tar.gz: 6c9419e7d8050fe6dd63a350f494c4668b06531d6871606aebeb1ecd887b389ad444d9bd80d96dd20f883b7434815162c2b98963b03db3fecb3e6e3eb2e68e47
6
+ metadata.gz: 78ac473ce4a0710031fd09b9f88f713e90838b287e8361b22ef1c534f8cb9d13369ea2ab1eb78a96e47ea7cfa04bed8014157891098f92743bf4f54113292596
7
+ data.tar.gz: dc3d33e8c76855b9e58eab1d258aa335f3d939688e2be0ee853a0614d901f61339204c358d866e8ec379318c0fd604e3eee2d4a3318a7d6a28cf9662bf6b2e9b
data/CHANGELOG.md CHANGED
@@ -1,10 +1,29 @@
1
- ## [Unreleased]
1
+ ## [0.1.5] - 2024-01-30
2
+ - Added callbacks to cache:
3
+ - Create/POST, refresh cache after successful POST request
4
+ - Update/PUT, refresh cache after successful PUT request
5
+ - Destroy/DELETE, invalidate cache after successful DELETE request
6
+ - Fixed issue with generator for SQLCache strategy tables
7
+ ## [0.1.4] - 2024-12-20
8
+ - CI Improvements
9
+ - Added annotations
10
+ - Runs tests from ActiveResource
11
+ - Separated Collection caching logic into ActiveCachedResource::Collection as opposed to monkey patching ActiveResource::Collection
12
+ - Changed name of SQL adapter from `active_record` to `active_record_sql`
13
+ - Changed method name of ActiveCachedResource::Model from `clear` to `clear_cache`
14
+
15
+ ## [0.1.3] - 2024-12-19
16
+ - Minor patch on ActiveResource. Removed deprecator log.
17
+
18
+ ## [0.1.2] - 2024-12-19
19
+ - Minor patch on ActiveResource::Collection initializing attributes.
2
20
 
3
21
  ## [0.1.1] - 2024-12-17
4
22
 
5
23
  - Added ruby yard documentation
6
24
  - Added LICENSE
7
25
  - Improved gemspec
26
+ - Changed name of ActiveSupport::Cache adapter from `active_support` to `active_support_cache`
8
27
 
9
28
 
10
29
  ## [0.1.0] - 2024-12-16
@@ -1,3 +1,5 @@
1
+ require_relative "collection"
2
+
1
3
  module ActiveCachedResource
2
4
  module Caching
3
5
  GLOBAL_PREFIX = "acr"
@@ -9,6 +11,10 @@ module ActiveCachedResource
9
11
  class << self
10
12
  alias_method :find_without_cache, :find
11
13
  alias_method :find, :find_with_cache
14
+
15
+ def collection_parser
16
+ _collection_parser || ActiveCachedResource::Collection
17
+ end
12
18
  end
13
19
  end
14
20
 
@@ -119,7 +125,7 @@ module ActiveCachedResource
119
125
  should_reload = options.delete(:reload) || !cached_resource.enabled
120
126
 
121
127
  # When bypassing cache, include the reload option as a query parameter for collection requests.
122
- # Hacky but this way ActiveResource::Collection#request_resources! can access it
128
+ # Hacky but this way ActiveCachedResource::Collection#request_resources! can access it
123
129
  if should_reload && args.first == :all
124
130
  options[:params] = {} if options[:params].blank?
125
131
  options[:params][RELOAD_PARAM] = should_reload
@@ -127,20 +133,20 @@ module ActiveCachedResource
127
133
  end
128
134
 
129
135
  if args.first == :all
130
- # Let ActiveResource::Collection handle the caching so that lazy loading is more effective
136
+ # Let ActiveCachedResource::Collection handle the caching so that lazy loading is more effective
131
137
  return find_via_reload(*args)
132
138
  end
133
139
 
134
140
  should_reload ? find_via_reload(*args) : find_via_cache(*args)
135
141
  end
136
142
 
137
- # Clears all cache matching the cache key prefix of the model.
138
- #
139
- # This method clears all cached entries that match the cache key prefix.
143
+ # Clears the cache for the specified pattern.
140
144
  #
145
+ # @param pattern [String, nil] The pattern to match cache keys against.
146
+ # If nil, all cache keys with this models prefix will be cleared.
141
147
  # @return [void]
142
- def clear
143
- cached_resource.cache.clear("#{cache_key_prefix}/")
148
+ def clear_cache(pattern = nil)
149
+ cached_resource.cache.clear("#{cache_key_prefix}/#{pattern}")
144
150
  end
145
151
 
146
152
  private
@@ -189,16 +195,16 @@ module ActiveCachedResource
189
195
 
190
196
  # Determines if the given object should be cached.
191
197
  #
192
- # @param object [Object, ActiveResource::Collection] The object to check for caching eligibility.
198
+ # @param object [Object, ActiveCachedResource::Collection] The object to check for caching eligibility.
193
199
  # @return [Boolean] Returns true if the object should be cached, false otherwise.
194
200
  def should_cache?(object)
195
201
  return false unless cached_resource.enabled
196
202
 
197
203
  # Calling `present?` on the `collection_parser`, an instance or descendent of
198
- # `ActiveResource::Collection` will trigger a request.
204
+ # `ActiveCachedResource::Collection` will trigger a request.
199
205
  # Checking if `requested?` first, will prevent an unnecessary network request when calling `present?`.
200
206
  case object
201
- when ActiveResource::Collection
207
+ when ActiveCachedResource::Collection
202
208
  object.requested? && object.present?
203
209
  else
204
210
  object.present?
@@ -0,0 +1,28 @@
1
+ module ActiveCachedResource
2
+ class Collection < ActiveResource::Collection
3
+ private
4
+
5
+ def request_resources!
6
+ return @elements if requested? || resource_class.nil?
7
+
8
+ # Delete the reload param from query params.
9
+ # This is drilled down via `params` option to determine if the collection should be reloaded
10
+ should_reload = query_params.delete(ActiveCachedResource::Caching::RELOAD_PARAM)
11
+ if !should_reload
12
+ from_cache = resource_class.send(:cache_read, from, path_params, query_params, prefix_options)
13
+ @elements = from_cache
14
+ return @elements if @elements
15
+ end
16
+
17
+ super # This sets @elements
18
+
19
+ if resource_class.send(:should_cache?, @elements)
20
+ resource_class.send(:cache_write, @elements, from, path_params, query_params, prefix_options)
21
+ end
22
+
23
+ @elements
24
+ ensure
25
+ @requested = true
26
+ end
27
+ end
28
+ end
@@ -7,7 +7,7 @@ require_relative "caching_strategies/base"
7
7
  module ActiveCachedResource
8
8
  class Configuration < OpenStruct
9
9
  CACHING_STRATEGIES = {
10
- active_record: ActiveCachedResource::CachingStrategies::SQLCache,
10
+ active_record_sql: ActiveCachedResource::CachingStrategies::SQLCache,
11
11
  active_support_cache: ActiveCachedResource::CachingStrategies::ActiveSupportCache
12
12
  }
13
13
 
@@ -18,7 +18,7 @@ module ActiveCachedResource
18
18
  # @param model [Class] The model class for which the configuration is being set.
19
19
  # @param options [Hash] A hash of options to customize the configuration.
20
20
  # @option options [Symbol] :cache_store The cache store to be used.
21
- # @option options [Symbol] :cache_strategy The cache strategy to be used. One of :active_record or :active_support_cache.
21
+ # @option options [Symbol] :cache_strategy The cache strategy to be used. One of :active_record_sql or :active_support_cache.
22
22
  # @option options [String] :cache_key_prefix The prefix for cache keys (default: model name underscored).
23
23
  # @option options [Logger] :logger The logger instance to be used (default: ActiveCachedResource::Logger).
24
24
  # @option options [Boolean] :enabled Whether caching is enabled (default: true).
@@ -8,6 +8,11 @@ module ActiveCachedResource
8
8
  extend ActiveSupport::Concern
9
9
 
10
10
  included do
11
+ before_save :invalidate_cache
12
+
13
+ after_save :save_to_cache
14
+ after_destroy :invalidate_cache
15
+
11
16
  class << self
12
17
  attr_accessor :cached_resource
13
18
 
@@ -15,7 +20,7 @@ module ActiveCachedResource
15
20
  #
16
21
  # @param options [Hash] A hash of options to customize the configuration.
17
22
  # @option options [Symbol] :cache_store The cache store to be used. Must be a CachingStrategies::Base instance.
18
- # @option options [Symbol] :cache_strategy The cache strategy to be used. One of :active_record or :active_support_cache.
23
+ # @option options [Symbol] :cache_strategy The cache strategy to be used. One of :active_record_sql or :active_support_cache.
19
24
  # @option options [String] :cache_key_prefix The prefix for cache keys (default: model name underscored).
20
25
  # @option options [Logger] :logger The logger instance to be used (default: ActiveCachedResource::Logger).
21
26
  # @option options [Boolean] :enabled Whether caching is enabled (default: true).
@@ -47,5 +52,15 @@ module ActiveCachedResource
47
52
  end
48
53
  end
49
54
  # :nodoc:
55
+
56
+ private
57
+
58
+ def invalidate_cache
59
+ self.class.clear_cache(id.to_s)
60
+ end
61
+
62
+ def save_to_cache
63
+ self.class.send(:cache_write, self, id)
64
+ end
50
65
  end
51
66
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveCachedResource
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.5"
5
5
  end
@@ -12,53 +12,4 @@ module ActiveResource
12
12
  class Base
13
13
  include ActiveCachedResource::Model
14
14
  end
15
-
16
- class Collection
17
- private
18
-
19
- # Monkey patch ActiveResource::Collection to handle caching
20
- # @see lib/activeresource/lib/active_resource/collection.rb
21
- def request_resources!
22
- return @elements if requested?
23
-
24
- # MONKEY PATCH
25
- # Delete the reload param from query params.
26
- # This is drilled down via `params` option to determine if the collection should be reloaded
27
- should_reload = query_params.delete(ActiveCachedResource::Caching::RELOAD_PARAM)
28
- if !should_reload
29
- from_cache = resource_class.send(:cache_read, from, path_params, query_params, prefix_options)
30
- @elements = from_cache
31
- return @elements if @elements
32
- end
33
- # MONKEY PATCH
34
-
35
- response =
36
- case from
37
- when Symbol
38
- resource_class.get(from, path_params)
39
- when String
40
- path = "#{from}#{query_string(query_params)}"
41
- resource_class.format.decode(resource_class.connection.get(path, resource_class.headers).body)
42
- else
43
- path = resource_class.collection_path(prefix_options, query_params)
44
- resource_class.format.decode(resource_class.connection.get(path, resource_class.headers).body)
45
- end
46
-
47
- # Update the elements
48
- parse_response(response)
49
- @elements.map! { |e| resource_class.instantiate_record(e, prefix_options) }
50
-
51
- # MONKEY PATCH
52
- # Write cache
53
- resource_class.send(:cache_write, @elements, from, path_params, query_params, prefix_options)
54
- @elements
55
- # MONKEY PATCH
56
- rescue ActiveResource::ResourceNotFound
57
- # Swallowing ResourceNotFound exceptions and return nothing - as per ActiveRecord.
58
- # Needs to be empty array as Array methods are delegated
59
- []
60
- ensure
61
- @requested = true
62
- end
63
- end
64
15
  end
@@ -202,7 +202,7 @@ module ActiveResource # :nodoc:
202
202
  #
203
203
  # [Array<Object>] The collection of resources retrieved from the API.
204
204
  def request_resources!
205
- return @elements if requested?
205
+ return @elements if requested? || resource_class.nil?
206
206
  response =
207
207
  case from
208
208
  when Symbol
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_cached_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
- original_platform: ''
7
6
  authors:
8
7
  - Jean Luis Urena
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-12-19 00:00:00.000000000 Z
10
+ date: 2025-01-09 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activemodel-serializers-xml
@@ -86,6 +85,7 @@ files:
86
85
  - lib/active_cached_resource/caching_strategies/active_support_cache.rb
87
86
  - lib/active_cached_resource/caching_strategies/base.rb
88
87
  - lib/active_cached_resource/caching_strategies/sql_cache.rb
88
+ - lib/active_cached_resource/collection.rb
89
89
  - lib/active_cached_resource/configuration.rb
90
90
  - lib/active_cached_resource/logger.rb
91
91
  - lib/active_cached_resource/model.rb
@@ -117,7 +117,7 @@ files:
117
117
  - lib/activeresource/lib/active_resource/threadsafe_attributes.rb
118
118
  - lib/activeresource/lib/active_resource/validations.rb
119
119
  - lib/activeresource/lib/activeresource.rb
120
- - lib/generators/active_cached_resource/install_generator.rb
120
+ - lib/generators/active_cached_resource/active_record_generator.rb
121
121
  - lib/generators/active_cached_resource/templates/migration.erb
122
122
  homepage: https://github.com/jlurena/active_cached_resource
123
123
  licenses: []
@@ -141,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
141
  - !ruby/object:Gem::Version
142
142
  version: '0'
143
143
  requirements: []
144
- rubygems_version: 3.6.1
144
+ rubygems_version: 3.6.2
145
145
  specification_version: 4
146
146
  summary: ActiveResource, but with a caching layer.
147
147
  test_files: []