counted_cache 0.2.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
+ SHA1:
3
+ metadata.gz: d37ddff712233dcdc2d0dc5c5eb32d5743eacb35
4
+ data.tar.gz: 96a06deee11bf404fbb0d9d52af88de817525b3b
5
+ SHA512:
6
+ metadata.gz: '038f467b83415bb5dca95b04dc81b866a9bb10b71dbb878ffd979df66ed2c1f0aac53e2df306efec49198489af6f9ff77ac84de628f7b7ff51e8a62860bb8467'
7
+ data.tar.gz: b913faf39839f0e7fc7267e7878b90817006b8b34e188ac7715fd5f48cbe8667464c649ba174e84a07a8806e6c2c5b75a49acd11385b0fdfd6b14fa9eaaf0464
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at peter.c.camilleri@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in counted_cache.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 PeterCamilleri
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,103 @@
1
+ # CountedCache
2
+
3
+ The counted_cache gem. A cache for use in cases where data is mostly only
4
+ read and where the cost of rebuilding that data is high, and yet, the size
5
+ of the data is too large to keep all of it in RAM all of the time.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'counted_cache'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install counted_cache
22
+
23
+ ## Usage
24
+
25
+ Creating a counted cache is as simple as:
26
+
27
+ ```ruby
28
+ cache_on_hand = CountedCache.new(10) {|key| retrieve_data_for(key) }
29
+ ```
30
+
31
+ Where:
32
+
33
+ * The argument is the optional cache depth, which is the number of data
34
+ items kept in the cache.
35
+ * The required block is used to fetch the data associated with the key when
36
+ that data is needed, but not currently in the cache.
37
+
38
+ So how do we use a counted cache to get at our data? That is really simple:
39
+
40
+ ```ruby
41
+ data = cache_on_hand[key]
42
+ ```
43
+
44
+ That's it! And that is the complete essentials guide. There are a very few
45
+ extras to determine how well the cache is working. The hits and misses
46
+ properties record how many times the data was found in the cache (a hit) and
47
+ how many times it had to be retrieved (a miss).
48
+
49
+ ### Removing data from the cache.
50
+
51
+ At some point, data items will need to be removed from the cache to make room
52
+ for newer data requests.
53
+
54
+ Normally when this happens, the old data can go away and no further action
55
+ needs to be taken. In some cases however, modified data will need to be written
56
+ out to save any changes that may have been made to that data. This is done by
57
+ giving the cache a block that does the work of saving the data into wherever
58
+ is identified by the key.
59
+
60
+ ```ruby
61
+ cache_on_hand.set_save_block do |key, data|
62
+ # Code to save data at key goes here.
63
+ end
64
+ ```
65
+
66
+ ### Example
67
+
68
+ Consider the case of an application that uses embedded ruby (erb) files. This
69
+ could look like:
70
+
71
+ ```ruby
72
+ erb_cache = CountedCache.new {|name| ERB.new(IO.read(name))}
73
+
74
+ # Other code omitted.
75
+
76
+ view_text = erb_cache["home_page/my_file.html.erb"].result(a_binding)
77
+ ```
78
+
79
+ The benchmark in the demo folder examines just such a scenario.
80
+
81
+ ## Contributing
82
+ 1. Fork it
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create new Pull Request
87
+
88
+ OR...
89
+
90
+ * Make a suggestion by raising an
91
+ [issue](https://github.com/PeterCamilleri/counted_cache/issues)
92
+ . All ideas and comments are welcome.
93
+
94
+ ## License
95
+
96
+ The gem is available as open source under the terms of the
97
+ [MIT License](./LICENSE.txt).
98
+
99
+ ## Code of Conduct
100
+
101
+ Everyone interacting in the counted_cache project’s codebases, issue trackers,
102
+ chat rooms and mailing lists is expected to follow the
103
+ [code of conduct](./CODE_OF_CONDUCT.md).
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "counted_cache/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "counted_cache"
7
+ spec.version = CountedCache::VERSION
8
+ spec.authors = ["PeterCamilleri"]
9
+ spec.email = ["peter.c.camilleri@gmail.com"]
10
+
11
+ spec.summary = CountedCache::DESCRIPTION
12
+ spec.description = "A cache for read mostly data with a high cost of retrieval."
13
+ spec.homepage = "https://github.com/PeterCamilleri/counted_cache"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|docs)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec
21
+ .files
22
+ .reject { |f| f.downcase == 'exe/readme.md'}
23
+ .grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.17"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "minitest", "~> 5.0"
29
+ spec.add_development_dependency 'minitest_visible', "~> 0.1"
30
+ end
@@ -0,0 +1,79 @@
1
+ # The counted_cache class. A cache for use in cases where data is mostly only
2
+ # read and where the cost of rebuilding that data is high, and yet, the size
3
+ # of the data is too large to keep all of it in RAM all of the time.
4
+
5
+ require_relative 'counted_cache/counted_cache_item'
6
+ require_relative 'counted_cache/version'
7
+
8
+ class CountedCache
9
+
10
+ # How many data elements should be retained?
11
+ attr_reader :depth
12
+
13
+ # How many times was data found in the cache?
14
+ attr_reader :hits
15
+
16
+ # How many times was data not in the cache?
17
+ attr_reader :misses
18
+
19
+ # Setup the cache
20
+ def initialize(depth = 10, &load_block)
21
+ fail "A data loading block is required" unless block_given?
22
+
23
+ @load_block = load_block
24
+ @save_block = Proc.new {}
25
+ @key_space = Hash.new {|hash, key| hash[key] = CountedCacheItem.new(key)}
26
+ @data_space = Array.new
27
+ self.depth = depth
28
+ @hits = 0
29
+ @misses = 0
30
+ end
31
+
32
+ # Set up the optional block called to save data.
33
+ def set_save_block(&save_block)
34
+ @save_block = save_block
35
+ end
36
+
37
+ # Get a data item.
38
+ def [](key)
39
+ item = @key_space[key]
40
+
41
+ if item.empty?
42
+ item.data = @load_block.call(key)
43
+ adjust_cache(1)
44
+ @data_space << item
45
+ @misses += 1
46
+ else
47
+ @hits += 1
48
+ end
49
+
50
+ item.data
51
+ end
52
+
53
+ # Set the new depth.
54
+ def depth=(value)
55
+ value = value.to_i
56
+ fail "The depth must be greater than zero." if value < 1
57
+ @depth = value
58
+ adjust_cache(0)
59
+ end
60
+
61
+ # How many cache slots are free?
62
+ def free
63
+ @depth - @data_space.length
64
+ end
65
+
66
+ private
67
+
68
+ # Make sure the data space has at least one free slot.
69
+ def adjust_cache(reserve)
70
+ return if free >= reserve
71
+
72
+ @data_space.sort_by!(&:count)
73
+
74
+ while free != reserve
75
+ @data_space.shift.purge(@save_block)
76
+ end
77
+ end
78
+
79
+ end
@@ -0,0 +1,36 @@
1
+ # A container for items in a counted cache.
2
+ class CountedCacheItem
3
+
4
+ # A marker for empty items.
5
+ EMPTY = :counted_cache_item_empty
6
+
7
+ # The reference count of this item.
8
+ attr_reader :count
9
+
10
+ # Let the data be set.
11
+ attr_writer :data
12
+
13
+ # Setup an empty data item.
14
+ def initialize(key)
15
+ @data = EMPTY
16
+ @key = key
17
+ @count = 0
18
+ end
19
+
20
+ # Is this item empty?
21
+ def empty?
22
+ @data == EMPTY
23
+ end
24
+
25
+ # Erase the data associated with the given key.
26
+ def purge(save_block)
27
+ save_block.call(@key, @data)
28
+ @data = EMPTY
29
+ end
30
+
31
+ # Retrieve the data, maintain a reference count.
32
+ def data
33
+ @count += 1
34
+ @data
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ class CountedCache
2
+ VERSION = "0.2.0".freeze
3
+
4
+ DESCRIPTION = "counted_cache: A cache for mostly read data with a high cost of retrieval.".freeze
5
+ end
data/rakefile.rb ADDED
@@ -0,0 +1,16 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
11
+
12
+ desc "What version of counted_cache is this?"
13
+ task :vers do |t|
14
+ puts
15
+ puts "counted_cache version = #{::CountedCache::VERSION}"
16
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: counted_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - PeterCamilleri
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest_visible
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.1'
69
+ description: A cache for read mostly data with a high cost of retrieval.
70
+ email:
71
+ - peter.c.camilleri@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - CODE_OF_CONDUCT.md
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - counted_cache.gemspec
82
+ - lib/counted_cache.rb
83
+ - lib/counted_cache/counted_cache_item.rb
84
+ - lib/counted_cache/version.rb
85
+ - rakefile.rb
86
+ homepage: https://github.com/PeterCamilleri/counted_cache
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.5.2
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: 'counted_cache: A cache for mostly read data with a high cost of retrieval.'
110
+ test_files: []