diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 446a7a1..45ab209 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby-version: ['3.2', '3.1.3', '3.0.5', '2.7.4'] + ruby-version: ['4.0'] steps: - name: Checkout code uses: actions/checkout@v3 diff --git a/.ruby-version b/.ruby-version index e4604e3..7636e75 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -3.2.1 +4.0.5 diff --git a/Gemfile.lock b/Gemfile.lock index fda455b..703ebde 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,77 +1,92 @@ PATH remote: . specs: - collectionspace-refcache (1.0.0) + collectionspace-refcache (1.0.1) redis (~> 4.2.1) zache (~> 0.12.0) GEM remote: https://rubygems.org/ specs: - ast (2.4.2) + ast (2.4.3) coderay (1.1.3) - diff-lcs (1.5.0) - docile (1.4.0) - json (2.6.3) - language_server-protocol (3.17.0.3) - method_source (1.0.0) + diff-lcs (1.6.2) + io-console (0.9.4) + json (3.1.0) + language_server-protocol (3.17.0.6) + lint_roller (1.1.0) + method_source (1.1.0) mock_redis (0.29.0) ruby2_keywords - parallel (1.22.1) - parser (3.2.1.1) + parallel (2.3.0) + parser (3.3.13.0) ast (~> 2.4.1) - pry (0.14.2) + racc + prism (1.9.0) + pry (0.16.1) coderay (~> 1.1) method_source (~> 1.0) + reline (>= 0.6.0) + racc (1.8.1) rainbow (3.1.1) - rake (13.0.6) + rake (13.4.2) redis (4.2.5) - regexp_parser (2.7.0) - rexml (3.3.9) - rspec (3.12.0) - rspec-core (~> 3.12.0) - rspec-expectations (~> 3.12.0) - rspec-mocks (~> 3.12.0) - rspec-core (3.12.1) - rspec-support (~> 3.12.0) - rspec-expectations (3.12.2) + regexp_parser (2.13.1) + reline (0.7.0) + io-console (~> 0.5) + rspec (3.13.2) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.6) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.5) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.12.0) - rspec-mocks (3.12.3) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.8) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.12.0) - rspec-support (3.12.0) - rubocop (1.44.1) - json (~> 2.3) - parallel (~> 1.10) - parser (>= 3.2.0.0) + rspec-support (~> 3.13.0) + rspec-support (3.13.7) + rubocop (1.91.0) + json (>= 2.3) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.1.0) + parallel (>= 1.10) + parser (>= 3.3.0.2) rainbow (>= 2.2.2, < 4.0) - regexp_parser (>= 1.8, < 3.0) - rexml (>= 3.2.5, < 4.0) - rubocop-ast (>= 1.24.1, < 2.0) + regexp_parser (>= 2.9.3, < 3.0) + rubocop-ast (>= 1.49.0, < 2.0) ruby-progressbar (~> 1.7) - unicode-display_width (>= 2.4.0, < 3.0) - rubocop-ast (1.27.0) - parser (>= 3.2.1.0) - rubocop-performance (1.15.2) - rubocop (>= 1.7.0, < 2.0) - rubocop-ast (>= 0.4.0) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.50.0) + parser (>= 3.3.7.2) + prism (~> 1.7) + rubocop-performance (1.26.1) + lint_roller (~> 1.1) + rubocop (>= 1.75.0, < 2.0) + rubocop-ast (>= 1.47.1, < 2.0) ruby-progressbar (1.13.0) ruby2_keywords (0.0.5) - simplecov (0.22.0) - docile (~> 1.1) - simplecov-html (~> 0.11) - simplecov_json_formatter (~> 0.1) - simplecov-html (0.12.3) - simplecov_json_formatter (0.1.4) - standard (1.24.3) + simplecov (1.3.2) + standard (1.57.0) language_server-protocol (~> 3.17.0.2) - rubocop (= 1.44.1) - rubocop-performance (= 1.15.2) - unicode-display_width (2.4.2) + lint_roller (~> 1.0) + rubocop (~> 1.91.0) + standard-custom (~> 1.0.0) + standard-performance (~> 1.8) + standard-custom (1.0.2) + lint_roller (~> 1.0) + rubocop (~> 1.50) + standard-performance (1.9.0) + lint_roller (~> 1.1) + rubocop-performance (~> 1.26.0) + unicode-display_width (3.3.0) + unicode-emoji (~> 4.3) + unicode-emoji (4.3.0) zache (0.12.0) PLATFORMS + arm64-darwin-25 ruby DEPENDENCIES @@ -81,8 +96,47 @@ DEPENDENCIES rake (~> 13.0) rspec rubocop - simplecov (~> 0.21) + simplecov standard +CHECKSUMS + ast (2.4.3) sha256=954615157c1d6a382bc27d690d973195e79db7f55e9765ac7c481c60bdb4d383 + coderay (1.1.3) sha256=dc530018a4684512f8f38143cd2a096c9f02a1fc2459edcfe534787a7fc77d4b + collectionspace-refcache (1.0.1) + diff-lcs (1.6.2) sha256=9ae0d2cba7d4df3075fe8cd8602a8604993efc0dfa934cff568969efb1909962 + io-console (0.9.4) sha256=a403304f32928ee85e56d457b4612ca43f2703b561a3258844992bd24085fbb1 + json (3.1.0) sha256=3ce7ccf8fde58960177a3a756c31f292a4c1e1dedee884547096a5b0604e3bd8 + language_server-protocol (3.17.0.6) sha256=5ef2c0c138f8267e1bc631d3328347d354f96724b0af22f2c79516120443b7f0 + lint_roller (1.1.0) sha256=2c0c845b632a7d172cb849cc90c1bce937a28c5c8ccccb50dfd46a485003cc87 + method_source (1.1.0) sha256=181301c9c45b731b4769bc81e8860e72f9161ad7d66dd99103c9ab84f560f5c5 + mock_redis (0.29.0) sha256=296956fff24a8bf10d771e4f1a54913161de480b879f53f7ae5030aa323c5282 + parallel (2.3.0) sha256=f75a3e904101ce6a1ccb6b8dc800cbafd42166d83330192438dcf29395167a6f + parser (3.3.13.0) sha256=e02f9b63c2fe05b0c68f692ba4df89b3cb113b663964f75d988fbebfd6c3deca + prism (1.9.0) sha256=7b530c6a9f92c24300014919c9dcbc055bf4cdf51ec30aed099b06cd6674ef85 + pry (0.16.1) sha256=d92d82c86e7f2dc34beec6d2892d5cf5ea517eea22eed3ceabb38ee62a98accc + racc (1.8.1) sha256=4a7f6929691dbec8b5209a0b373bc2614882b55fc5d2e447a21aaa691303d62f + rainbow (3.1.1) sha256=039491aa3a89f42efa1d6dec2fc4e62ede96eb6acd95e52f1ad581182b79bc6a + rake (13.4.2) sha256=cb825b2bd5f1f8e91ca37bddb4b9aaf345551b4731da62949be002fa89283701 + redis (4.2.5) sha256=88a85e4a379b8fdfdc76518dd4fd5daa34464735785ef4f6929924004dd6a297 + regexp_parser (2.13.1) sha256=5aedb6b7c35688f51e86eef17a15374dfd287c83f4ca644b2c5f3ec50a33b44b + reline (0.7.0) sha256=5b012d8e55dbf9d450f12bde2cf7d15ff546ae80b3f8f3b30e570d431815583d + rspec (3.13.2) sha256=206284a08ad798e61f86d7ca3e376718d52c0bc944626b2349266f239f820587 + rspec-core (3.13.6) sha256=a8823c6411667b60a8bca135364351dda34cd55e44ff94c4be4633b37d828b2d + rspec-expectations (3.13.5) sha256=33a4d3a1d95060aea4c94e9f237030a8f9eae5615e9bd85718fe3a09e4b58836 + rspec-mocks (3.13.8) sha256=086ad3d3d17533f4237643de0b5c42f04b66348c28bf6b9c2d3f4a3b01af1d47 + rspec-support (3.13.7) sha256=0640e5570872aafefd79867901deeeeb40b0c9875a36b983d85f54fb7381c47c + rubocop (1.91.0) sha256=9c82b7bf391c5d7e3798c5b9996e22a1fe3bd7468e351dfdeb96140c058296d0 + rubocop-ast (1.50.0) sha256=b9ca88300da0803ee222ad20cdb30494c0a784eed06fdc35d254b06d662788db + rubocop-performance (1.26.1) sha256=cd19b936ff196df85829d264b522fd4f98b6c89ad271fa52744a8c11b8f71834 + ruby-progressbar (1.13.0) sha256=80fc9c47a9b640d6834e0dc7b3c94c9df37f08cb072b7761e4a71e22cff29b33 + ruby2_keywords (0.0.5) sha256=ffd13740c573b7301cf7a2e61fc857b2a8e3d3aff32545d6f8300d8bae10e3ef + simplecov (1.3.2) sha256=2a22ec1759f1b8db60881a2e038758956fc69ef93ed669be0bde0678de6d0e85 + standard (1.57.0) sha256=4a65ea92421fbaf77fb54da91e16093cf79b335b8f62c35aaa3dd714c8592858 + standard-custom (1.0.2) sha256=424adc84179a074f1a2a309bb9cf7cd6bfdb2b6541f20c6bf9436c0ba22a652b + standard-performance (1.9.0) sha256=49483d31be448292951d80e5e67cdcb576c2502103c7b40aec6f1b6e9c88e3f2 + unicode-display_width (3.3.0) sha256=4b7aa66a4b11db50f6f7e98411215cd0dd10eaecb665d36eec5bcacb3fa0b613 + unicode-emoji (4.3.0) sha256=11c02fa73290378c066bb0562cd4c87d8e1b706fbbe0059ca12746a4244de8ce + zache (0.12.0) sha256=6a3bccae54ca88fa0b0afa9debdbcb30c3f3b470d8763844c10a9fa62b02bce7 + BUNDLED WITH - 2.4.8 + 4.0.10 diff --git a/README.md b/README.md index 9fb304f..7135d8c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# CollectionSpace RefCache +# CollectionSpace Refcache A caching system for CollectionSpace refnames. diff --git a/Rakefile b/Rakefile index ec6d8ed..614ba1b 100644 --- a/Rakefile +++ b/Rakefile @@ -8,5 +8,5 @@ RSpec::Core::RakeTask.new(:spec) task default: :spec task :version do - puts CollectionSpace::RefCache::VERSION + puts CollectionSpace::Refcache::VERSION end diff --git a/collectionspace-refcache.gemspec b/collectionspace-refcache.gemspec index 87758b4..aa25624 100644 --- a/collectionspace-refcache.gemspec +++ b/collectionspace-refcache.gemspec @@ -4,15 +4,15 @@ require_relative "lib/collectionspace/refcache/version" Gem::Specification.new do |spec| spec.name = "collectionspace-refcache" - spec.version = CollectionSpace::RefCache::VERSION + spec.version = CollectionSpace::Refcache::VERSION spec.authors = ["Mark Cooper"] spec.email = ["mark.c.cooper@outlook.com"] - spec.summary = "CollectionSpace RefCache." + spec.summary = "CollectionSpace Refcache." spec.description = "A caching system for CollectionSpace refnames." spec.homepage = "https://github.org/collectionspace/collectionspace-refcache" spec.license = "MIT" - spec.required_ruby_version = ">= 2.7.4" + spec.required_ruby_version = ">= 4.0.5" spec.metadata["homepage_uri"] = spec.homepage spec.metadata["source_code_uri"] = spec.homepage @@ -32,6 +32,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency("rake", "~> 13.0") spec.add_development_dependency("rspec") spec.add_development_dependency("rubocop") - spec.add_development_dependency("simplecov", "~> 0.21") + spec.add_development_dependency("simplecov") spec.add_development_dependency("standard") end diff --git a/doc/REFCACHE.md b/doc/REFCACHE.md index cd1ebd0..0b85c3a 100644 --- a/doc/REFCACHE.md +++ b/doc/REFCACHE.md @@ -1,15 +1,18 @@ -# RefCache +# Refcache Setup and usage: ```ruby +# In-memory cache (Zache) will be used as backend if neither :redis nor :store are included +# in config cache_config = { - # redis: 'redis://localhost:6379/1', # optional, if omitted use in memory cache (Zache) + # redis: 'redis://localhost:6379/1', # optional + # store: ActiveSupport::Cache::Store or SolidCache::Store object, # optional domain: 'core.collectionspace.org', error_if_not_found: false, # raise error if key cannot be retrieved (default false) lifetime: 5 * 60, # cache expiry in seconds (default is 5 minutes) } -cache = CollectionSpace::RefCache.new(config: cache_config) +cache = CollectionSpace::Refcache.new(config: cache_config) cache.get('placeauthorities', 'place', 'Death Valley') # $refname or error / nil if not found cache.exists?('placeauthorities', 'place', 'Death Valley') # check for key diff --git a/lib/collectionspace/refcache/backend.rb b/lib/collectionspace/refcache/backend.rb index 57e1bdf..072168d 100644 --- a/lib/collectionspace/refcache/backend.rb +++ b/lib/collectionspace/refcache/backend.rb @@ -1,10 +1,11 @@ # frozen_string_literal: true +require_relative "backend/activesupport_cache_store" require_relative "backend/redis" require_relative "backend/zache" module CollectionSpace - class RefCache + class Refcache module Backend end end diff --git a/lib/collectionspace/refcache/backend/activesupport_cache_store.rb b/lib/collectionspace/refcache/backend/activesupport_cache_store.rb new file mode 100644 index 0000000..98ffacd --- /dev/null +++ b/lib/collectionspace/refcache/backend/activesupport_cache_store.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +module CollectionSpace + class Refcache + module Backend + class ActivesupportCacheStore + # @param store [ActiveSupport::Cache::Store] or other store conforming + # to its abstract interface (e.g. SolidCache::Store) + def initialize(store) + @c = store + end + + def clean + return nil unless @c.respond_to?(:clean) + + @c.clean + end + + def connected? + "PONG" # cute, matches redis response + end + + def exists?(key) + @c.exist?(key) + end + + def flush + return nil unless @c.respond_to?(:clear) + + @c.clear + end + + def get(key) + @c.read(key) + end + + def put(key, value, lifetime: nil) + if lifetime + @c.write(key, value, expires_in: lifetime) + else + @c.write(key, value) + end + end + + def remove(key) + @c.delete(key) + end + + def size + return nil unless @c.respond_to?(:stats) + + @c.stats.dig(:connection_stats, :cache, :entries) + end + end + end + end +end diff --git a/lib/collectionspace/refcache/backend/redis.rb b/lib/collectionspace/refcache/backend/redis.rb index 1bf8dd6..f95cb82 100644 --- a/lib/collectionspace/refcache/backend/redis.rb +++ b/lib/collectionspace/refcache/backend/redis.rb @@ -3,7 +3,7 @@ require "redis" module CollectionSpace - class RefCache + class Refcache module Backend class Redis def initialize(url) diff --git a/lib/collectionspace/refcache/backend/zache.rb b/lib/collectionspace/refcache/backend/zache.rb index c572955..86a3b99 100644 --- a/lib/collectionspace/refcache/backend/zache.rb +++ b/lib/collectionspace/refcache/backend/zache.rb @@ -3,7 +3,7 @@ require "zache" module CollectionSpace - class RefCache + class Refcache module Backend class Zache def initialize diff --git a/lib/collectionspace/refcache/refcache.rb b/lib/collectionspace/refcache/refcache.rb index 87dab28..55170a7 100644 --- a/lib/collectionspace/refcache/refcache.rb +++ b/lib/collectionspace/refcache/refcache.rb @@ -3,14 +3,14 @@ require "digest" module CollectionSpace - class RefCache + class Refcache class NotFoundError < StandardError; end attr_reader :config, :domain def initialize(config: {}) @config = config - @cache = backend(config.fetch(:redis, nil)) + @cache = set_backend(config) @domain = config.fetch(:domain) @error_if_not_found = config.fetch(:error_if_not_found, false) @lifetime = config.fetch(:lifetime, 5 * 60) @@ -190,8 +190,14 @@ def generic_exists?(key) @cache.exists?(key) end - def backend(connection) - connection ? Backend::Redis.new(connection) : Backend::Zache.new + def set_backend(config) + if config.key?(:redis) + Backend::Redis.new(config[:redis]) + elsif config.key?(:store) + Backend::ActivesupportCacheStore.new(config[:store]) + else + Backend::Zache.new + end end def generate_key(parts = []) diff --git a/lib/collectionspace/refcache/version.rb b/lib/collectionspace/refcache/version.rb index 22c260f..6361a31 100644 --- a/lib/collectionspace/refcache/version.rb +++ b/lib/collectionspace/refcache/version.rb @@ -2,8 +2,8 @@ module CollectionSpace # rubocop:disable Style/StaticClass - class RefCache - VERSION = "1.0.0" + class Refcache + VERSION = "1.0.1" end # rubocop:enable Style/StaticClass end diff --git a/spec/refcache/refcache_spec.rb b/spec/refcache/refcache_spec.rb index f50d83e..683bf9b 100644 --- a/spec/refcache/refcache_spec.rb +++ b/spec/refcache/refcache_spec.rb @@ -2,7 +2,7 @@ require "mock_redis" -RSpec.describe CollectionSpace::RefCache do +RSpec.describe CollectionSpace::Refcache do let(:base_config) { {domain: "core.collectionspace.org"} } let(:add_config) { {} } let(:config) { base_config.merge(add_config) } @@ -24,16 +24,16 @@ end end - # The backends are tested in the context of RefCache's methods because: + # The backends are tested in the context of Refcache's methods because: # - The backend methods are not available in the interface to be called directly (without using # dirty Ruby tricks anyway) - # - The RefCache methods are the interface to the backends and it is important that they + # - The Refcache methods are the interface to the backends and it is important that they # work as expected context "when zache backend" do describe "#initialize" do it "returns Zache cache" do c = cache.instance_variable_get(:@cache) - expect(c).to be_a(CollectionSpace::RefCache::Backend::Zache) + expect(c).to be_a(CollectionSpace::Refcache::Backend::Zache) end end @@ -84,7 +84,7 @@ let(:result) { cache.get("a", "b", "c") } it "raises NotFoundError" do - expect { result }.to raise_error(CollectionSpace::RefCache::NotFoundError) + expect { result }.to raise_error(CollectionSpace::Refcache::NotFoundError) end end end @@ -127,7 +127,7 @@ describe "#initialize" do it "returns Redis cache" do c = cache.instance_variable_get(:@cache) - expect(c).to be_a(CollectionSpace::RefCache::Backend::Redis) + expect(c).to be_a(CollectionSpace::Refcache::Backend::Redis) end end @@ -177,7 +177,7 @@ let(:result) { cache.get("a", "b", "c") } it "raises NotFoundError" do - expect { result }.to raise_error(CollectionSpace::RefCache::NotFoundError) + expect { result }.to raise_error(CollectionSpace::Refcache::NotFoundError) end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8ebb1ed..1b43625 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,7 +6,7 @@ require "bundler/setup" require "collectionspace/refcache" require "pry" -require_relative "./helpers" +require_relative "helpers" RSpec.configure do |config| config.include Helpers