evanescence 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a6f2dca734bfb023b9f2c927e49904ca5c34c4247ec619ce704d766ad01f8d66
4
+ data.tar.gz: c336919947cecc092f7d8e1c4495e4f46097d65bc92ed446c36b64b7c90dbe0c
5
+ SHA512:
6
+ metadata.gz: 5dd994c61a90b4932917924e347866ba92d60d93431ebc559848e1c43adde7c99bd3e50d6fceb348fc70a837573bc8e8a0a937c991e66afc86fce5c6c70b5f39
7
+ data.tar.gz: 12ab15620128dda67b0b53480bb7ddea55a0f11769aac037a3b4f3e98c2cb07e7a2e01fab75ce34a0d931f3ea6617ee0176c46ab5fe82a6d8c2876bbc328fc68
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,37 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+ NewCops: enable
4
+
5
+ Gemspec/DevelopmentDependencies:
6
+ Enabled: false
7
+
8
+ Layout/HashAlignment:
9
+ EnforcedColonStyle: table
10
+ EnforcedHashRocketStyle: table
11
+ Layout/LineLength:
12
+ Max: 120
13
+ Layout/SpaceInsideHashLiteralBraces:
14
+ EnforcedStyle: no_space
15
+ Lint/AssignmentInCondition:
16
+ Enabled: false
17
+
18
+ Style/BlockDelimiters:
19
+ EnforcedStyle: braces_for_chaining
20
+ Style/ConditionalAssignment:
21
+ Enabled: false
22
+ Style/Documentation:
23
+ Enabled: false
24
+ Style/FrozenStringLiteralComment:
25
+ Enabled: false
26
+ Style/MultilineBlockChain:
27
+ Enabled: false
28
+ Style/PercentLiteralDelimiters:
29
+ PreferredDelimiters:
30
+ '%i': ()
31
+ '%w': ()
32
+ Style/StringLiterals:
33
+ Enabled: true
34
+ EnforcedStyle: double_quotes
35
+ Style/StringLiteralsInInterpolation:
36
+ Enabled: true
37
+ EnforcedStyle: double_quotes
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Florin Diconescu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # Evanescence
2
+
3
+ [![Push & PR](https://github.com/florindiconescu/evanescence/actions/workflows/main.yml/badge.svg)](https://github.com/florindiconescu/evanescence/actions/workflows/main.yml)
4
+
5
+ In memory cache which can limit the cache size by a maximum number by expiring the least used cached items.
6
+
7
+ ## Usage
8
+
9
+ Initiate a cache object. Optionally pass the max number of records as an argument:
10
+ ```
11
+ cache = Evanescence.initialize_cache(max_size: 1000)
12
+ ```
13
+ Write to cache: `cache.write("key", value)`
14
+
15
+ Read from cache: `cache.read("key")`
16
+
17
+ Delete from cache: `cache.delete("key")`
18
+
19
+ Delete the entire cache: `cache.clear`
20
+
21
+ Check the cache size: `cache.count`
22
+
23
+ ## Next steps
24
+ Expire the cache after given time.
25
+
26
+ Enhance the API.
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/evanescence/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "evanescence"
7
+ spec.version = Evanescence::VERSION
8
+ spec.authors = ["florindiconescu"]
9
+ spec.email = ["florin.diconescu@gmail.com"]
10
+
11
+ spec.summary = "Simple in memory cache"
12
+ spec.homepage = "https://github.com/florindiconescu/evanescence"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.1", "< 3.4"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(__dir__) do
21
+ `git ls-files -z`.split("\x0").reject do |f|
22
+ (File.expand_path(f) == __FILE__) ||
23
+ f.start_with?(*%w(bin/ test/ spec/ features/ .git .github appveyor Gemfile))
24
+ end
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "rspec", "~> 3.2"
31
+ spec.add_development_dependency "rubocop", "1.60.2"
32
+ spec.add_development_dependency "rubocop-rspec", "2.26.1"
33
+ spec.metadata["rubygems_mfa_required"] = "true"
34
+ end
@@ -0,0 +1,61 @@
1
+ module Evanescence
2
+ # Simple implementation of caching
3
+ class Cache
4
+ def initialize(max_size:)
5
+ @items = {}
6
+ @max_size = max_size
7
+ end
8
+
9
+ def to_h
10
+ items.transform_values(&:first)
11
+ end
12
+
13
+ def write(key, val)
14
+ reject_least_used_item if max_size
15
+ existing_item = items[key]
16
+ existing_item ? update_value(existing_item, val) : items[key] = [val, 1]
17
+
18
+ items[key].first
19
+ end
20
+
21
+ def read(key)
22
+ item = items[key]
23
+ return unless item
24
+
25
+ item[1] += 1 if max_size
26
+ item.first
27
+ end
28
+
29
+ def delete(key)
30
+ items.delete(key)
31
+ end
32
+
33
+ def clear
34
+ count_before_clear = count
35
+ items.clear
36
+ count_before_clear
37
+ end
38
+
39
+ def count
40
+ items.count
41
+ end
42
+
43
+ private
44
+
45
+ attr_reader :items, :max_size
46
+
47
+ def update_value(item, value)
48
+ item.tap do |i|
49
+ i.first = value
50
+ i[1] += 1 if max_size
51
+ end
52
+ end
53
+
54
+ def reject_least_used_item
55
+ return if items.size < max_size
56
+
57
+ min = items.min_by { |_, v| v.last }
58
+ delete(min[0])
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Evanescence
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,11 @@
1
+ require_relative "evanescence/version"
2
+
3
+ module Evanescence
4
+ autoload :Cache, "evanescence/cache"
5
+
6
+ module_function
7
+
8
+ def initialize_cache(max_size: nil)
9
+ Cache.new(max_size:)
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ module Evanescence
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: evanescence
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - florindiconescu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.60.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.60.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 2.26.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 2.26.1
55
+ description:
56
+ email:
57
+ - florin.diconescu@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".rspec"
63
+ - ".rubocop.yml"
64
+ - LICENSE.txt
65
+ - README.md
66
+ - evanescence.gemspec
67
+ - lib/evanescence.rb
68
+ - lib/evanescence/cache.rb
69
+ - lib/evanescence/version.rb
70
+ - sig/evanescence.rbs
71
+ homepage: https://github.com/florindiconescu/evanescence
72
+ licenses:
73
+ - MIT
74
+ metadata:
75
+ homepage_uri: https://github.com/florindiconescu/evanescence
76
+ rubygems_mfa_required: 'true'
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '3.1'
86
+ - - "<"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.4'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.5.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Simple in memory cache
99
+ test_files: []