Skip to content

Commit dfd8e31

Browse files
committed
replacing the moneta adapter with pstore implementation
moneta seems to be an overkill for our use case. we can do well with pstore instead for storing the key value pairs into file while staying compatible with faraday's http cache interface. this change also adds the pstore gem to gemspec and pins it to ~>0.1.1 because that's the version that was shipped with ruby 3.1.*. Signed-off-by: Rishi Kumar Chawda <rishichawda@users.noreply.github.com>
1 parent 7cd4ac3 commit dfd8e31

File tree

6 files changed

+45
-41
lines changed

6 files changed

+45
-41
lines changed

components/ruby/Gemfile.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ PATH
66
faraday (>= 1, < 2)
77
faraday-http-cache
88
mixlib-log (~> 3.0)
9-
moneta (~> 1.6)
109
ostruct (~> 0.1.0)
10+
pstore (~> 0.1.1)
1111
tty-prompt (~> 0.23)
1212
tty-spinner (~> 0.9.3)
1313

@@ -105,6 +105,7 @@ GEM
105105
pry (0.15.2)
106106
coderay (~> 1.1)
107107
method_source (~> 1.0)
108+
pstore (0.1.4)
108109
public_suffix (6.0.2)
109110
racc (1.8.1)
110111
rainbow (3.1.1)

components/ruby/chef-licensing.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ Gem::Specification.new do |spec|
3030
spec.add_dependency "tty-prompt", "~> 0.23"
3131
spec.add_dependency "faraday", ">= 1", "< 2"
3232
spec.add_dependency "faraday-http-cache"
33-
spec.add_dependency "moneta", "~> 1.6"
3433
spec.add_dependency "tty-spinner", "~> 0.9.3"
3534
spec.add_dependency "mixlib-log", "~> 3.0"
36-
35+
3736
# Gem dependency needed with Ruby 3.4 upgrade
3837
spec.add_dependency "ostruct", "~> 0.1.0"
38+
spec.add_dependency "pstore", "~> 0.1.1"
3939
end

components/ruby/lib/chef-licensing/moneta_adapter.rb

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require "pstore"
2+
3+
module ChefLicensing
4+
# Simple adapter to make PStore compatible with Faraday::HttpCache
5+
class PStoreAdapter
6+
def initialize(cache_dir)
7+
@store_path = File.join(cache_dir, "chef_licensing_cache.pstore")
8+
@store = PStore.new(@store_path)
9+
end
10+
11+
# Interface methods required by faraday-http-cache
12+
def read(key)
13+
@store.transaction { @store[key] }
14+
end
15+
16+
def write(key, value, options = {})
17+
# PStore handles persistence, Faraday::HttpCache handles
18+
# HTTP cache headers for expiration
19+
@store.transaction { @store[key] = value }
20+
end
21+
22+
def delete(key)
23+
@store.transaction { @store.delete(key) }
24+
end
25+
26+
def exist?(key)
27+
@store.transaction { @store.root?(key) }
28+
end
29+
30+
def clear
31+
@store.transaction do
32+
@store.roots.each { |root| @store.delete(root) }
33+
end
34+
end
35+
end
36+
end

components/ruby/lib/chef-licensing/restful_client/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require_relative "../exceptions/restful_client_connection_error"
66
require_relative "../exceptions/missing_api_credentials_error"
77
require_relative "../config"
8-
require_relative "../moneta_adapter"
8+
require_relative "../pstore_adapter"
99
require_relative "middleware/exceptions_handler"
1010
require_relative "middleware/content_type_validator"
1111

@@ -132,7 +132,7 @@ def handle_post_connection(url = nil)
132132
end
133133

134134
def get_connection(url = nil)
135-
store = MonetaAdapter.new(Dir.tmpdir)
135+
store = PStoreAdapter.new(Dir.tmpdir)
136136
Faraday.new(url: url) do |config|
137137
config.request :json
138138
config.response :json, parser_options: { object_class: OpenStruct }

components/ruby/spec/chef-licensing/activesupport_replacement_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require "spec_helper"
2-
require "chef-licensing/moneta_adapter"
2+
require "chef-licensing/pstore_adapter"
33
require "chef-licensing/string_refinements"
44

5-
RSpec.describe ChefLicensing::MonetaAdapter do
5+
RSpec.describe ChefLicensing::PStoreAdapter do
66
let(:cache_dir) { Dir.mktmpdir }
77
let(:adapter) { described_class.new(cache_dir) }
88

@@ -51,7 +51,7 @@
5151
end
5252
end
5353

54-
RSpec.describe ChefLicensing::MonetaAdapter do
54+
RSpec.describe ChefLicensing::PStoreAdapter do
5555
let(:cache_dir) { Dir.mktmpdir }
5656
let(:adapter) { described_class.new(cache_dir) }
5757

0 commit comments

Comments
 (0)