swiftlru 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4ad894a77607231a46678167d7e2709474e98ca5cedc6e4a757e94b1cb23eb94
4
+ data.tar.gz: 8426919dccd2ca3f4e59b86a6cfcbd2c5d0197ca44f12b6d7bf0e939c698f20e
5
+ SHA512:
6
+ metadata.gz: 7947589bcee4fbee8851a937a4b1d30912d654fc2e0a9355a9182b524e9ed6a5c738f7ab742567d2fe03b9957708120d11820c023150677cfeeb61a590630ec0
7
+ data.tar.gz: 894da3818bd6ad6258ec6dee884682afcfe853d7928e76834e27c42d659a5bee72089750654bf6f2c3abceb919dc926cba5ea4882ba78ad2f93797956846d265
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SwiftLRU
4
+ # A tiny, fast LRU cache
5
+ class Cache
6
+ attr_reader :max_size, :size
7
+
8
+ # Initializes a new Cache object with the specified maximum size.
9
+ #
10
+ # @param max_size [Integer] The maximum size of the cache. Must be an integer greater than 0.
11
+ # @raise [ArgumentError] if max_size is not an integer greater than 0.
12
+ def initialize(max_size)
13
+ raise ArgumentError, 'max_size must be an integer greater than 0' if !max_size.is_a?(Integer) || max_size < 1
14
+
15
+ @size = 0
16
+ @max_size = max_size
17
+ @cache = {}
18
+ @old_cache = {}
19
+ end
20
+
21
+ # Checks if the cache contains the specified key.
22
+ #
23
+ # @param key [Object] The key to check.
24
+ # @return [Boolean] true if the cache contains the key, false otherwise.
25
+ def has(key)
26
+ @cache.key?(key) || @old_cache.key?(key)
27
+ end
28
+
29
+ # Removes the specified key from the cache.
30
+ #
31
+ # @param key [Object] The key to remove.
32
+ # @return [void]
33
+ def remove(key)
34
+ if @cache.key?(key)
35
+ @cache.delete(key)
36
+ @size -= 1
37
+ end
38
+
39
+ return unless @old_cache.key?(key)
40
+
41
+ @old_cache.delete(key)
42
+ end
43
+
44
+ # Retrieves the value associated with the specified key from the cache.
45
+ #
46
+ # @param key [Object] The key to retrieve the value for.
47
+ # @return [Object, nil] The value associated with the key, or nil if the key is not found.
48
+ def get(key)
49
+ return @cache[key] if @cache.key?(key)
50
+
51
+ if @old_cache.key?(key)
52
+ value = @old_cache[key]
53
+ update(key, value)
54
+ return value
55
+ end
56
+
57
+ nil
58
+ end
59
+
60
+ # Sets the value associated with the specified key in the cache.
61
+ #
62
+ # @param key [Object] The key to set the value for.
63
+ # @param value [Object] The value to set.
64
+ # @return [void]
65
+ def set(key, value)
66
+ if @cache.key?(key)
67
+ @cache[key] = value
68
+ @size += 1
69
+ else
70
+ update(key, value)
71
+ end
72
+ end
73
+
74
+ # Clears the cache, removing all keys and values.
75
+ #
76
+ # @return [void]
77
+ def clear
78
+ @cache = {}
79
+ @old_cache = {}
80
+ @size = 0
81
+ end
82
+
83
+ private
84
+
85
+ def update(key, value)
86
+ @cache[key] = value
87
+ @size += 1
88
+
89
+ return unless @size >= @max_size
90
+
91
+ @old_cache = @cache
92
+ @cache = {}
93
+ @size = 0
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SwiftLRU
4
+ VERSION = '0.1.0'
5
+ end
data/lib/swiftlru.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'swiftlru/version'
4
+
5
+ # :nodoc:
6
+ module SwiftLRU
7
+ autoload :Cache, 'swiftlru/cache'
8
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swiftlru
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yatharth K
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-12-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Least Recently Used (LRU) cache in Ruby designed to be fast and simple.
14
+ email: yatharth01@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/swiftlru.rb
20
+ - lib/swiftlru/cache.rb
21
+ - lib/swiftlru/version.rb
22
+ homepage: https://rubygems.org/gems/swiftlru
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 2.7.0
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.4.21
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: A tiny, fast LRU cache
45
+ test_files: []