Skip to content

Commit

Permalink
Refactor caching (#271)
Browse files Browse the repository at this point in the history
* Improves handling of caching
* improved documentation
  • Loading branch information
olbrich authored Oct 2, 2022
1 parent 3ee71f2 commit 3bddb6f
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 211 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -53,7 +53,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -67,4 +67,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v2
2 changes: 1 addition & 1 deletion .solargraph.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ exclude:
require: []
domains: []
reporters:
- rubocop
- require_not_found
- typecheck
require_paths: []
plugins: []
max_files: 5000
18 changes: 9 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ GEM
racc (1.6.0-java)
rainbow (3.1.1)
rake (13.0.6)
rb-fsevent (0.11.1)
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
redcarpet (3.5.1)
regexp_parser (2.5.0)
regexp_parser (2.6.0)
reline (0.3.1)
io-console (~> 0.5)
reverse_markdown (2.1.1)
Expand All @@ -91,14 +91,14 @@ GEM
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
rspec-expectations (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-support (3.11.0)
rubocop (1.35.1)
rspec-support (3.11.1)
rubocop (1.36.0)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.1.2.1)
Expand All @@ -112,8 +112,8 @@ GEM
parser (>= 3.1.1.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (2.12.1)
rubocop (~> 1.31)
rubocop-rspec (2.13.2)
rubocop (~> 1.33)
ruby-maven (3.3.13)
ruby-maven-libs (~> 3.3.9)
ruby-maven-libs (3.3.9)
Expand All @@ -126,7 +126,7 @@ GEM
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
solargraph (0.46.0)
solargraph (0.47.2)
backport (~> 1.2)
benchmark
bundler (>= 1.17.2)
Expand All @@ -147,7 +147,7 @@ GEM
terminal-notifier-guard (1.7.0)
thor (1.2.1)
tilt (2.0.11)
unicode-display_width (2.2.0)
unicode-display_width (2.3.0)
webrick (1.7.0)
yard (0.9.28)
webrick (~> 1.7.0)
Expand Down
41 changes: 27 additions & 14 deletions lib/ruby_units/cache.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
module RubyUnits
class Unit < Numeric
@@cached_units = {}
# Performance optimizations to avoid creating units unnecessarily
class Cache
attr_accessor :data

class Cache
def self.get(key = nil)
key.nil? ? @@cached_units : @@cached_units[key]
end
def initialize
clear
end

# @param key [String, #to_unit]
# @return [RubyUnits::Unit, nil]
def get(key)
key = key&.to_unit&.units unless key.is_a?(String)
data[key]
end

def self.set(key, value)
@@cached_units[key] = value
end
# @param key [String, #to_unit]
# @return [void]
def set(key, value)
key = key.to_unit.units unless key.is_a?(String)
data[key] = value
end

# @return [Array<String>]
def keys
data.keys
end

def self.clear
@@cached_units = {}
@@base_unit_cache = {}
Unit.new(1)
end
# Reset the cache
def clear
@data = {}
end
end
end
16 changes: 8 additions & 8 deletions lib/ruby_units/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# allow for optional configuration of RubyUnits
#
# Usage:
#
# RubyUnits.configure do |config|
# config.separator = false
# end
module RubyUnits
class << self
attr_writer :configuration
Expand All @@ -18,6 +11,13 @@ def self.reset
@configuration = Configuration.new
end

# allow for optional configuration of RubyUnits
#
# Usage:
#
# RubyUnits.configure do |config|
# config.separator = false
# end
def self.configure
yield configuration
end
Expand All @@ -34,7 +34,7 @@ def initialize
end

def separator=(value)
raise ArgumentError, "configuration 'separator' may only be true or false" unless value == true || value == false
raise ArgumentError, "configuration 'separator' may only be true or false" unless [true, false].include?(value)

@separator = value ? ' ' : nil
end
Expand Down
Loading

0 comments on commit 3bddb6f

Please sign in to comment.