lru_qache 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 11dd210b06332d830d3f6d469f13309b4a56fd62ad3bc769c1a336c6b8e9c084
4
- data.tar.gz: 2d622b4f930b5720095e4ecd8229bc765dec5898e23cc048b3a4e154fdccb8af
3
+ metadata.gz: ce54ab1353d2238480ec292912794f944f54346c01ef056e09b0d423b0a4b523
4
+ data.tar.gz: 3a7717a5d6c8d3b59a4c1a153b2d5b2748a44c356bf76a02e7cae5acc9cd8abb
5
5
  SHA512:
6
- metadata.gz: ae6fe8df75d35676d7690825f1e5a3b6557ae23668103c1ed5ba7257fd4a5756dfb5642c60fab82cd2ed6c9cc7f49bd6b874ce7ada5a0f14f86cd2703ea00402
7
- data.tar.gz: 6e510d47e587a9aa18bc5ae8320d3fb7b539c61d47e8bedd3c7b93aaed2a890c083b405fe96bf9fecde8328f240372b194447dc8b0fbf388566dd8336018d1a2
6
+ metadata.gz: 29cc291ab8a88eb1de5d07217a632de206e3d9ecda03a4ea86bcb0b12e8c46254999d5dc3be8527c838f55b4b536cf2b802e664f8fad31722edb4f825b1b613f
7
+ data.tar.gz: c37522b2aefa7c9b6aecde01472db8c46b05677572425f33b73e4e6fa6d8079f856d02ed53210968523fa9e1ed596b8f34de7ac97aaad1d0dcb4959f227935d6
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # LRUQache
2
2
 
3
- The LRU(least recently used) caching scheme is to remove the least recently used item when the cache reaches it's defined capacity. This gem implements this cache using a simple customised implementation of queue. So the gem is LRU Qache i.e. LRU Queue Cache.
3
+ The LRU(least recently used) caching scheme is to remove the least recently used item when the cache reaches it's defined capacity. This gem implements this cache using a core Hash which is [ordered hash](https://www.igvita.com/2009/02/04/ruby-19-internals-ordered-hash/). But this will work only if Ruby version is greater than 1.9.
4
4
 
5
5
  ## Installation
6
6
 
@@ -1,13 +1,12 @@
1
- require "lru_qache/lru_queue"
2
-
3
- class LRUQache # LRU cache is caching technique based on recently used data.
1
+ # require "lru_qache/lru_queue"
2
+ class LruQache # LRU cache is caching technique based on recently used data.
4
3
  MAX_CAPACITY = 5
5
4
  DEFAULT_VAL = -1
6
5
 
7
6
  InvalidCapacityError = Class.new RuntimeError
8
7
  INVALID_CAPACITY = 'Capacity must be valid number'.freeze
9
8
 
10
- attr_reader :options, :cache
9
+ attr_reader :options, :cache, :capacity
11
10
 
12
11
  # @param capacity [Integer] Takes a number as an input and
13
12
  # @param options [Hash] optional hash for some settings like custom value if \
@@ -17,7 +16,7 @@ class LRUQache # LRU cache is caching technique based on recently used data.
17
16
  raise InvalidLimitError, INVALID_CAPACITY unless capacity.is_a?(Integer)
18
17
  @cache = {}
19
18
  @options = options
20
- @lru_queue = LRUQueue.new(capacity)
19
+ @capacity = capacity
21
20
  end
22
21
 
23
22
  # This gets the value based on the key is provided, updates the key usage
@@ -26,9 +25,9 @@ class LRUQache # LRU cache is caching technique based on recently used data.
26
25
  # @return value [Object] if key is present
27
26
  # @example get(x)
28
27
  def get(key)
29
- return options[:default_val] if cache[key].nil?
30
- update_queue(key)
31
- retrieve(key)
28
+ value = retrieve(key)
29
+ update_lru(key) unless value == options[:default_val]
30
+ value
32
31
  end
33
32
 
34
33
  # Sets the value of cache's key
@@ -36,46 +35,41 @@ class LRUQache # LRU cache is caching technique based on recently used data.
36
35
  # @param key any valid object
37
36
  # @param key any valid object
38
37
  # @example set('a', 1)
39
-
40
38
  # @todo Add validation to the key e.g. only Symbol, String or Integer etc.
41
39
  def set(key, val)
42
- remove_lru_if_needed(key)
40
+ @cache.delete(key) if cache.has_key?(key)
43
41
  @cache[key] = val
42
+ remove_lru_if_needed
43
+ val
44
44
  end
45
45
 
46
46
  alias [] get
47
47
  alias []= set
48
48
 
49
49
  private
50
-
51
- # Logic to handle whether to set a key, now it only checks if key present
52
- #
53
- # @param Takes key the input, it can be any valid Object needed for hash
54
- def need_to_set?(key)
55
- retrieve(key).nil?
56
- end
57
-
58
50
  # This is used for not modifying the 'last_used' key when using internal logic
59
51
  #
60
52
  # @param key input parameter
61
53
  # @return value [Object] if key is present
62
54
  def retrieve(key)
63
- cache[key]
55
+ cache.fetch(key) { options[:default_val] }
64
56
  end
65
57
 
66
58
  # This removes the least recently used key from cache and also updates queue
67
59
  #
68
60
  # @param key input parameter
69
- def remove_lru_if_needed(key)
70
- update_queue(key) # update the queue with new key
71
- @cache.delete(@lru_queue.last_popped) if @lru_queue.full?
61
+ def remove_lru_if_needed
62
+ # update the queue with new key
72
63
  # remove LRU key if capacity is full.
64
+ cache.shift if cache.size > capacity
73
65
  end
74
66
 
75
- # Updates the queue with the key
67
+ # Updates the lru with the key, removes key and sets again so key goes
68
+ # to the last
76
69
  #
77
70
  # @param Takes key the input, it can be any valid Object needed for hash
78
- def update_queue(key)
79
- @lru_queue.push(key)
71
+ def update_lru(key)
72
+ val = cache.delete(key)
73
+ cache[key] = val
80
74
  end
81
75
  end
@@ -1,3 +1,3 @@
1
1
  module LRUQache
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -5,15 +5,15 @@ Gem::Specification.new do |spec|
5
5
  spec.version = LRUQache::VERSION
6
6
  spec.date = '2020-08-03'
7
7
  spec.summary = 'LRU Cache using a queue.'
8
- spec.description = 'A simple LRU(Least Recently Used) Cache implementation using a custom queue.'
8
+ spec.description = 'A simple LRU(Least Recently Used) Cache implementation using Ruby Hash'
9
9
  spec.authors = ['Datt Dongare']
10
10
  spec.email = 'duttdongare30@gmail.com'
11
11
  spec.homepage = 'https://rubygems.org/gems/lru-qache'
12
- spec.metadata['homepage_uri'] = 'https://rubygems.org/gems/lru-qache'
12
+ spec.metadata['homepage_uri'] = 'https://rubygems.org/gems/lru_qache'
13
13
  spec.metadata['source_code_uri'] = 'https://github.com/datt/lru_qache'
14
14
  spec.require_paths = ['lib']
15
15
  spec.license = 'MIT'
16
- spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
16
+ spec.required_ruby_version = Gem::Requirement.new('>= 1.9')
17
17
  # Specify which files should be added to the gem when it is released.
18
18
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
19
  spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lru_qache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Datt Dongare
@@ -38,8 +38,7 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.9.25
41
- description: A simple LRU(Least Recently Used) Cache implementation using a custom
42
- queue.
41
+ description: A simple LRU(Least Recently Used) Cache implementation using Ruby Hash
43
42
  email: duttdongare30@gmail.com
44
43
  executables: []
45
44
  extensions: []
@@ -53,14 +52,13 @@ files:
53
52
  - bin/console
54
53
  - bin/setup
55
54
  - lib/lru_qache.rb
56
- - lib/lru_qache/lru_queue.rb
57
55
  - lib/lru_qache/version.rb
58
56
  - lru_qache.gemspec
59
57
  homepage: https://rubygems.org/gems/lru-qache
60
58
  licenses:
61
59
  - MIT
62
60
  metadata:
63
- homepage_uri: https://rubygems.org/gems/lru-qache
61
+ homepage_uri: https://rubygems.org/gems/lru_qache
64
62
  source_code_uri: https://github.com/datt/lru_qache
65
63
  post_install_message:
66
64
  rdoc_options: []
@@ -70,7 +68,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
68
  requirements:
71
69
  - - ">="
72
70
  - !ruby/object:Gem::Version
73
- version: 2.3.0
71
+ version: '1.9'
74
72
  required_rubygems_version: !ruby/object:Gem::Requirement
75
73
  requirements:
76
74
  - - ">="
@@ -1,34 +0,0 @@
1
- class LRUQueue # Maintains a fixed size queue with unique keys
2
- attr_reader :max_size, :last_popped
3
- attr_accessor :queue
4
- ARG_ERROR = 'ArgError:: Passing Max Size is mandatory and must be a number' \
5
- .freeze
6
-
7
- # @param max_size [Integer] Takes a number as an input, for queue size
8
- def initialize(max_size)
9
- raise ARG_ERROR if max_size.nil? || !max_size.is_a?(Integer)
10
- @max_size = max_size
11
- @queue = []
12
- @last_popped = nil
13
- end
14
-
15
- # removes last item from the queue
16
- def pop
17
- @last_popped = queue.pop
18
- end
19
-
20
- # Pushes item in the queue based on some conditions
21
- # 1. If item is repeated it pushed at first, removes duplicate
22
- # 2. If full, remove least used item i.e. last item and push
23
- # @param item [Object] takes any object and pushes in queue
24
- def push(item)
25
- queue.delete(item) if queue.include?(item) # deletes the key if present
26
- pop if full? # delete last item if queue is full
27
- queue.unshift(item) # insert at the start
28
- end
29
-
30
- # tells whether the queue is full or not
31
- def full?
32
- max_size == queue.size
33
- end
34
- end