lru_qache 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 11dd210b06332d830d3f6d469f13309b4a56fd62ad3bc769c1a336c6b8e9c084
4
+ data.tar.gz: 2d622b4f930b5720095e4ecd8229bc765dec5898e23cc048b3a4e154fdccb8af
5
+ SHA512:
6
+ metadata.gz: ae6fe8df75d35676d7690825f1e5a3b6557ae23668103c1ed5ba7257fd4a5756dfb5642c60fab82cd2ed6c9cc7f49bd6b874ce7ada5a0f14f86cd2703ea00402
7
+ data.tar.gz: 6e510d47e587a9aa18bc5ae8320d3fb7b539c61d47e8bedd3c7b93aaed2a890c083b405fe96bf9fecde8328f240372b194447dc8b0fbf388566dd8336018d1a2
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in lru_qache.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Datt Dongare
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.
@@ -0,0 +1,54 @@
1
+ # LRUQache
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.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'lru_qache'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install lru_qache
20
+
21
+ ## Usage
22
+ ```ruby
23
+ require "lru_qache"
24
+
25
+ # create a new cache with a 100 items capacity.
26
+ cache = LRUQache.new(100)
27
+
28
+ # set an item in cache
29
+ cache.set('key', 'your_value')
30
+
31
+ cache.get('key')
32
+ # returns 'your_value'
33
+
34
+ ```
35
+
36
+ ### Documentation
37
+
38
+ `YARD` is used for generating documentation
39
+ Generate documentation using yardoc `lib/**/*.rb` and open doc/index.html in browser.
40
+
41
+
42
+ ### Test
43
+ To run the test cases
44
+
45
+ `rspec spec/*`
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/datt/lru_qache.
50
+
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lru_qache"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,81 @@
1
+ require "lru_qache/lru_queue"
2
+
3
+ class LRUQache # LRU cache is caching technique based on recently used data.
4
+ MAX_CAPACITY = 5
5
+ DEFAULT_VAL = -1
6
+
7
+ InvalidCapacityError = Class.new RuntimeError
8
+ INVALID_CAPACITY = 'Capacity must be valid number'.freeze
9
+
10
+ attr_reader :options, :cache
11
+
12
+ # @param capacity [Integer] Takes a number as an input and
13
+ # @param options [Hash] optional hash for some settings like custom value if \
14
+ # key is not present
15
+ # @option options [Integer] :default_val
16
+ def initialize(capacity = MAX_CAPACITY, options = { default_val: DEFAULT_VAL })
17
+ raise InvalidLimitError, INVALID_CAPACITY unless capacity.is_a?(Integer)
18
+ @cache = {}
19
+ @options = options
20
+ @lru_queue = LRUQueue.new(capacity)
21
+ end
22
+
23
+ # This gets the value based on the key is provided, updates the key usage
24
+ #
25
+ # @param key input parameter
26
+ # @return value [Object] if key is present
27
+ # @example get(x)
28
+ def get(key)
29
+ return options[:default_val] if cache[key].nil?
30
+ update_queue(key)
31
+ retrieve(key)
32
+ end
33
+
34
+ # Sets the value of cache's key
35
+ #
36
+ # @param key any valid object
37
+ # @param key any valid object
38
+ # @example set('a', 1)
39
+
40
+ # @todo Add validation to the key e.g. only Symbol, String or Integer etc.
41
+ def set(key, val)
42
+ remove_lru_if_needed(key)
43
+ @cache[key] = val
44
+ end
45
+
46
+ alias [] get
47
+ alias []= set
48
+
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
+ # This is used for not modifying the 'last_used' key when using internal logic
59
+ #
60
+ # @param key input parameter
61
+ # @return value [Object] if key is present
62
+ def retrieve(key)
63
+ cache[key]
64
+ end
65
+
66
+ # This removes the least recently used key from cache and also updates queue
67
+ #
68
+ # @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?
72
+ # remove LRU key if capacity is full.
73
+ end
74
+
75
+ # Updates the queue with the key
76
+ #
77
+ # @param Takes key the input, it can be any valid Object needed for hash
78
+ def update_queue(key)
79
+ @lru_queue.push(key)
80
+ end
81
+ end
@@ -0,0 +1,34 @@
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
@@ -0,0 +1,3 @@
1
+ module LRUQache
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ require_relative 'lib/lru_qache/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "lru_qache"
5
+ spec.version = LRUQache::VERSION
6
+ spec.date = '2020-08-03'
7
+ spec.summary = 'LRU Cache using a queue.'
8
+ spec.description = 'A simple LRU(Least Recently Used) Cache implementation using a custom queue.'
9
+ spec.authors = ['Datt Dongare']
10
+ spec.email = 'duttdongare30@gmail.com'
11
+ spec.homepage = 'https://rubygems.org/gems/lru-qache'
12
+ spec.metadata['homepage_uri'] = 'https://rubygems.org/gems/lru-qache'
13
+ spec.metadata['source_code_uri'] = 'https://github.com/datt/lru_qache'
14
+ spec.require_paths = ['lib']
15
+ spec.license = 'MIT'
16
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.add_development_dependency 'rspec', '~> 3.9'
23
+ spec.add_development_dependency 'yard', '~> 0.9.25'
24
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lru_qache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Datt Dongare
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-08-03 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.25
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.25
41
+ description: A simple LRU(Least Recently Used) Cache implementation using a custom
42
+ queue.
43
+ email: duttdongare30@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - bin/console
54
+ - bin/setup
55
+ - lib/lru_qache.rb
56
+ - lib/lru_qache/lru_queue.rb
57
+ - lib/lru_qache/version.rb
58
+ - lru_qache.gemspec
59
+ homepage: https://rubygems.org/gems/lru-qache
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ homepage_uri: https://rubygems.org/gems/lru-qache
64
+ source_code_uri: https://github.com/datt/lru_qache
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 2.3.0
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 3.0.8
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: LRU Cache using a queue.
84
+ test_files: []