Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE-71] Hash keys and serialize/compress payload responses #72

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,21 @@ end
### Options
CachedResource accepts the following options as a hash:

| Option | Description | Default |
|----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------|
| `:enabled` | Enables or disables caching. | `true` |
| `:cache_collections` | Set to false to always remake a request for collections. | `true` |
| `:cache` | The cache store that CacheResource should use. | The `Rails.cache` if available, or an `ActiveSupport::Cache::MemoryStore` |
| `:cache_key_prefix` | A prefix to be added to the cache keys. | `nil` |
| `:collection_arguments` | The arguments that identify the principal collection request. | `[:all]` |
| `:collection_synchronize` | Use collections to generate cache entries for individuals. Update the existing cached principal collection when retrieving subsets of the principal collection or individuals. | `false` |
| `:logger` | The logger to which CachedResource messages should be written. | The `Rails.logger` if available, or an `ActiveSupport::Logger` |
| `:race_condition_ttl` | The race condition ttl, to prevent [dog pile effect](https://en.wikipedia.org/wiki/Cache_stampede) or [cache stampede](https://en.wikipedia.org/wiki/Cache_stampede). | `86400` |
| `:ttl_randomization_scale` | A Range from which a random value will be selected to scale the ttl. | `1..2` |
| `:ttl_randomization` | Enable ttl randomization. | `false` |
| `:ttl` | The time in seconds until the cache should expire. | `604800` |
| Option | Description | Default |
|----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------|
| `:enabled` | Enables or disables caching. | `true` |
| `:cache_collections` | Set to `false` to always remake a request for collections. | `true` |
| `:cache` | The cache store that CachedResource should use. | `Rails.cache` (if available), or an `ActiveSupport::Cache::MemoryStore`. |
| `:cache_key_prefix` | A prefix to be added to the cache keys. | `nil` |
| `:collection_arguments` | The arguments that identify the principal collection request. | `[:all]` |
| `:collection_synchronize` | Use collections to generate cache entries for individuals. Updates the cached principal collection when retrieving subsets or individuals. | `false` |
| `:compress` | Whether to compress cache values using the [`msgpack`](https://rubygems.org/gems/msgpack) gem. The `msgpack` gem must be part of your dependencies. | `false` |
| `:logger` | The logger to which CachedResource messages should be written. | `Rails.logger` (if available), or an `ActiveSupport::Logger`. |
| `:race_condition_ttl` | The race condition TTL, to prevent the [dog-pile effect](https://en.wikipedia.org/wiki/Cache_stampede) or [cache stampede](https://en.wikipedia.org/wiki/Cache_stampede). | `86400` |
| `:max_key_length` | Sets a max key length. Keys exceeding this length will be hashed into a 256-character hex key. | `nil` (keys are never hashed). |
| `:ttl_randomization_scale` | A range from which a random value is selected to scale the TTL. | `1..2` |
| `:ttl_randomization` | Enables TTL randomization. | `false` |
| `:ttl` | The time in seconds until the cache should expire. | `604800` |

For example:
```ruby
Expand Down Expand Up @@ -144,7 +146,6 @@ To automatically apply linter fixes: `bundle exec rake standard:fix`

## Credit/Inspiration
* quamen and [this gist](http://gist.github.com/947734)
* latimes and [this plugin](http://github.com/latimes/cached_resource)

## Feedback/Problems
Feedback is greatly appreciated! Check out this project's [issue tracker](https://github.com/Ahsizara/cached_resource/issues) if you've got anything to say.
4 changes: 2 additions & 2 deletions cached_resource.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ Gem::Specification.new do |s|

s.required_ruby_version = ">= 3.0"

s.add_runtime_dependency "activeresource", ">= 4.0"
s.add_runtime_dependency "activesupport", ">= 4.0"
s.add_runtime_dependency "activeresource", ">= 6.1"
s.add_runtime_dependency "nilio", ">= 1.0"

s.add_development_dependency "concurrent-ruby"
s.add_development_dependency "msgpack", "~> 1.7", ">= 1.7.3"
s.add_development_dependency "pry-byebug"
s.add_development_dependency "rake"
s.add_development_dependency "rspec"
Expand Down
3 changes: 2 additions & 1 deletion lib/cached_resource.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "msgpack"
require "nilio"
require "ostruct"

require "nilio"
require "active_support/cache"
require "active_support/concern"
require "active_support/logger"
Expand Down
1 change: 1 addition & 0 deletions lib/cached_resource/cached_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def cached_resource(options = {})
# Set up cached resource for this class by creating a new configuration
# and establishing the necessary methods.
def setup_cached_resource!(options)
require "msgpack" if options[:compress]
@cached_resource = CachedResource::Configuration.new(options)
send :include, CachedResource::Caching
@cached_resource
Expand Down
13 changes: 10 additions & 3 deletions lib/cached_resource/caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ def is_any_collection?(*arguments)
# Read a entry from the cache for the given key.
def cache_read(key)
object = cached_resource.cache.read(key).try do |json_cache|
json = ActiveSupport::JSON.decode(json_cache)
json = cached_resource.compress ? MessagePack.unpack(json_cache) : json_cache
json = ActiveSupport::JSON.decode(json)

unless json.nil?
cache = json_to_object(json)
Expand All @@ -130,7 +131,9 @@ def cache_write(key, object, *arguments)
params = options[:params]
prefix_options, query_options = split_options(params)

result = cached_resource.cache.write(key, object_to_json(object, prefix_options, query_options), race_condition_ttl: cached_resource.race_condition_ttl, expires_in: cached_resource.generate_ttl)
serialized_json = object_to_json(object, prefix_options, query_options)
serialized_json = MessagePack.pack(serialized_json) if cached_resource.compress
result = cached_resource.cache.write(key, serialized_json, race_condition_ttl: cached_resource.race_condition_ttl, expires_in: cached_resource.generate_ttl)
result && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} WRITE #{key}")
result
end
Expand Down Expand Up @@ -158,7 +161,11 @@ def cache_key_delete_pattern

# Generate the request cache key.
def cache_key(*arguments)
"#{name_key}/#{arguments.join("/")}".downcase.delete(" ")
key = "#{name_key}/#{arguments.join("/")}".downcase.delete(" ")
if cached_resource.max_key_length && key.length > cached_resource.max_key_length
key = Digest::SHA256.hexdigest(key)
end
key
end

def name_key
Expand Down
11 changes: 8 additions & 3 deletions lib/cached_resource/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class Configuration < OpenStruct
# prefix for log messages
LOGGER_PREFIX = "[cached_resource]"

# Max key length
MAX_KEY_LENGTH = nil

# Initialize a Configuration with the given options, overriding any
# defaults. The following options exist for cached resource:
# :enabled, default: true
Expand All @@ -25,17 +28,19 @@ class Configuration < OpenStruct
# :cache_collections, default: true
def initialize(options = {})
super({
cache: defined?(Rails.cache) && Rails.cache || CACHE,
cache_collections: true,
cache_key_prefix: nil,
cache: defined?(Rails.cache) && Rails.cache || CACHE,
collection_arguments: [:all],
collection_synchronize: false,
compress: false,
enabled: true,
logger: defined?(Rails.logger) && Rails.logger || LOGGER,
max_key_length: options.fetch(:max_key_length, MAX_KEY_LENGTH),
race_condition_ttl: 86400,
ttl: 604800,
ttl_randomization_scale: 1..2,
ttl_randomization: false,
ttl_randomization_scale: 1..2
ttl: 604800
}.merge(options))
end

Expand Down
2 changes: 1 addition & 1 deletion lib/cached_resource/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module CachedResource
VERSION = "9.0.0"
VERSION = "9.1.0"
end
61 changes: 51 additions & 10 deletions spec/cached_resource/caching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ def read_from_cache(key, model = Thing)
cache: CACHE,
collection_arguments: [:all],
collection_synchronize: false,
compress: false,
enabled: true,
generate_ttl: 604800,
generate_ttl: 604_800,
logger: double(:thing_logger, info: nil, error: nil),
race_condition_ttl: 86400,
max_key_length: nil,
race_condition_ttl: 86_400,
ttl_randomization_scale: 1..2,
ttl_randomization: false,
ttl: 604800)
ttl: 604_800)
end

let(:not_the_thing_cached_resource) do
Expand All @@ -55,13 +57,15 @@ def read_from_cache(key, model = Thing)
cache: CACHE,
collection_arguments: [:all],
collection_synchronize: false,
compress: false,
enabled: true,
generate_ttl: 604800,
generate_ttl: 604_800,
logger: double(:not_the_thing_logger, info: nil, error: nil),
race_condition_ttl: 86400,
max_key_length: nil,
race_condition_ttl: 86_400,
ttl_randomization_scale: 1..2,
ttl_randomization: false,
ttl: 604800)
ttl: 604_800)
end

before do
Expand All @@ -88,6 +92,26 @@ def read_from_cache(key, model = Thing)
allow(not_the_thing_cached_resource).to receive(:enabled).and_return(true)
end

context "When a `max_key_length` is set" do
before do
allow(thing_cached_resource).to receive(:max_key_length).and_return(10)
end

context "When key length is greater than `max_key_length`" do
it "caches a response with hashed key" do
result = Thing.find(1, from: "path", params: {foo: "bar"})
expect(read_from_cache(Digest::SHA256.hexdigest('thing/1/{:from=>"path",:params=>{:foo=>"bar"}}'))).to eq(result)
end
end

context "When key length is less than `max_key_length`" do
it "caches a response with unhashed key" do
result = Thing.find(1)
expect(read_from_cache("thing/1")).to eq(result)
end
end
end

context "Caching single resource" do
it "caches a response" do
result = Thing.find(1)
Expand Down Expand Up @@ -195,12 +219,14 @@ def read_from_cache(key, model = Thing)

context "custom collection arguments" do
before do
allow(thing_cached_resource).to receive(:collection_arguments).and_return([:all, params: {name: 42}])
allow(thing_cached_resource).to receive(:collection_arguments).and_return([:all, {params: {name: 42}}])
end

it "checks for custom collection arguments" do
Thing.all
expect { Thing.find(:all, params: {name: 42}) }.to change(ActiveResource::HttpMock.requests, :length).from(1).to(2)
expect do
Thing.find(:all, params: {name: 42})
end.to change(ActiveResource::HttpMock.requests, :length).from(1).to(2)
end
end
end
Expand All @@ -225,6 +251,21 @@ def read_from_cache(key, model = Thing)
end
end

context "compress" do
before do
allow(thing_cached_resource).to receive(:compress).and_return(true)
allow(MessagePack).to receive(:pack).and_call_original
allow(MessagePack).to receive(:unpack).and_call_original
end

it "compresses the cache entry" do
result = Thing.find(1)
expect(read_from_cache("thing/1")).to eq(result)
expect(MessagePack).to have_received(:pack)
expect(MessagePack).to have_received(:unpack)
end
end

context "when cache prefix is set" do
context "cache_key_prefix is a string" do
before { allow(thing_cached_resource).to receive(:cache_key_prefix).and_return("prefix123") }
Expand Down Expand Up @@ -329,14 +370,14 @@ def read_from_cache(key, model = Thing)
context "with cache ActiveSupport::Cache::MemoryStore" do
let(:cache_class) { ActiveSupport::Cache::MemoryStore.new }
it do
expect(Thing.send(:cache_key_delete_pattern)).to eq(/^thing\//)
expect(Thing.send(:cache_key_delete_pattern)).to eq(%r{^thing/})
end
end

context "with cache ActiveSupport::Cache::FileStore" do
let(:cache_class) { ActiveSupport::Cache::FileStore.new("tmp/") }
it do
expect(Thing.send(:cache_key_delete_pattern)).to eq(/^thing\//)
expect(Thing.send(:cache_key_delete_pattern)).to eq(%r{^thing/})
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/cached_resource/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class Bar3 < Bar
expect(configuration.ttl).to eq(604800)
end

it "should have key length of Configuration::MAX_KEY_LENGTH" do
expect(configuration.max_key_length).to eq(CachedResource::Configuration::MAX_KEY_LENGTH)
end

it "should disable collection synchronization" do
expect(configuration.collection_synchronize).to eq(false)
end
Expand Down