barking_iguana-cache_bucket 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +6 -0
- data/barking_iguana-cache_bucket.gemspec +33 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/barking_iguana/cache_bucket.rb +33 -0
- data/lib/barking_iguana/cache_bucket/cache_bucket.rb +78 -0
- data/lib/barking_iguana/cache_bucket/dsl.rb +22 -0
- data/lib/barking_iguana/cache_bucket/version.rb +5 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c90c21ce4ef8a040fa432ce3f00a5359388039da
|
4
|
+
data.tar.gz: 499b20e6a2c0bd512fc110a8fb6e2f61491d1008
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8ee6d900f6a57a301115a604ddd4fd6cb0c23ef10e9bd9621491fa7b6677e0fd7de73c79c188019f21b8ec8446ea0837d6aa82fad094bdd3620df9d03294fa80
|
7
|
+
data.tar.gz: 17faf5648ae72761f58156c506b55965d50c40e170ed508db8827af80b57d680541a6077ee674d3bcb106b522e5b4819b32907d8eececb718441087174df2e92
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# BarkingIguana::CacheBucket
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/barking_iguana/cache_bucket`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'barking_iguana-cache_bucket'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install barking_iguana-cache_bucket
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/barking_iguana-cache_bucket.
|
36
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'barking_iguana/cache_bucket/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "barking_iguana-cache_bucket"
|
8
|
+
spec.version = BarkingIguana::CacheBucket::VERSION
|
9
|
+
spec.authors = ["Craig R Webster"]
|
10
|
+
spec.email = ["craig@barkingiguana.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Naive in-memory cache with expiry}
|
13
|
+
spec.description = %q{Naive in-memory cache with expiry}
|
14
|
+
spec.homepage = "https://github.com/barkingiguana/cache_bucket"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
17
|
+
# delete this section to allow pushing this gem to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
20
|
+
else
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
22
|
+
end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_dependency "null_logger"
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
33
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "barking_iguana/cache_bucket"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "barking_iguana/cache_bucket/version"
|
2
|
+
require "barking_iguana/cache_bucket/cache_bucket"
|
3
|
+
require "barking_iguana/cache_bucket/dsl"
|
4
|
+
|
5
|
+
module BarkingIguana
|
6
|
+
module CacheBucket
|
7
|
+
class << self
|
8
|
+
attr_accessor :logger
|
9
|
+
end
|
10
|
+
self.logger = NullLogger.instance
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_accessor :maximum_age
|
14
|
+
end
|
15
|
+
self.maximum_age = 3600
|
16
|
+
|
17
|
+
def self.get name, options = {}
|
18
|
+
buckets[name] ||= create_bucket name, options
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.buckets
|
22
|
+
@bucket ||= {}
|
23
|
+
end
|
24
|
+
private_class_method :buckets
|
25
|
+
|
26
|
+
def self.create_bucket name, options
|
27
|
+
o = options.dup
|
28
|
+
o[:logger] ||= logger
|
29
|
+
o[:maximum_age] ||= maximum_age
|
30
|
+
CacheBucket.new o
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'null_logger'
|
2
|
+
|
3
|
+
module BarkingIguana
|
4
|
+
module CacheBucket
|
5
|
+
class CacheBucket
|
6
|
+
KEY_DIVIDER = '__'.freeze
|
7
|
+
|
8
|
+
attr_accessor :maximum_age
|
9
|
+
private :maximum_age=, :maximum_age
|
10
|
+
|
11
|
+
attr_accessor :logger
|
12
|
+
private :logger=, :logger
|
13
|
+
|
14
|
+
attr_accessor :last_cleaned
|
15
|
+
private :last_cleaned=, :last_cleaned
|
16
|
+
|
17
|
+
attr_accessor :cache
|
18
|
+
private :cache=, :cache
|
19
|
+
|
20
|
+
def initialize maximum_age: 3600, logger: nil
|
21
|
+
self.maximum_age = maximum_age.to_i
|
22
|
+
self.logger = logger || NullLogger.instance
|
23
|
+
self.cache = {}
|
24
|
+
end
|
25
|
+
|
26
|
+
def get key
|
27
|
+
logger.debug { "Getting #{key}" }
|
28
|
+
evict_expired_keys
|
29
|
+
k = generation_key(key)
|
30
|
+
if cache.key? k
|
31
|
+
logger.debug { "Key #{k.inspect} found in the cache" }
|
32
|
+
cache[k].tap do |v|
|
33
|
+
logger.debug { "Value is #{v.inspect}" }
|
34
|
+
end
|
35
|
+
else
|
36
|
+
logger.debug { "Key #{k.inspect} is not in the cache" }
|
37
|
+
return unless block_given?
|
38
|
+
logger.debug { "I was passed a block to calculate the value" }
|
39
|
+
yield.tap do |v|
|
40
|
+
logger.debug { "Calculated value: #{v.inspect}" }
|
41
|
+
set key, v
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def set key, value
|
47
|
+
evict_expired_keys
|
48
|
+
k = generation_key(key)
|
49
|
+
logger.debug { "Setting value to #{value.inspect}" }
|
50
|
+
cache[k] = value
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def evict_expired_keys
|
56
|
+
g = generation_id
|
57
|
+
return if g == last_cleaned
|
58
|
+
logger.debug { "Performing cache maintenance at generation #{g}" }
|
59
|
+
count = cache.keys.size
|
60
|
+
cache.delete_if do |k, _v|
|
61
|
+
k !~ %r{^#{g}#{KEY_DIVIDER}}
|
62
|
+
end
|
63
|
+
logger.debug { "Deleted #{count - cache.keys.size} keys" }
|
64
|
+
self.last_cleaned = g
|
65
|
+
end
|
66
|
+
|
67
|
+
def generation_id
|
68
|
+
Time.now.to_i / maximum_age
|
69
|
+
end
|
70
|
+
|
71
|
+
def generation_key key
|
72
|
+
[ generation_id, key ].join(KEY_DIVIDER).tap do |k|
|
73
|
+
logger.debug { "Current generation key for #{key.inspect} is #{k.inspect}" }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module BarkingIguana
|
2
|
+
module CacheBucket
|
3
|
+
module DSL
|
4
|
+
def self.included into
|
5
|
+
into.extend ClassMethods
|
6
|
+
into.include InstanceMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module InstanceMethods
|
10
|
+
def class_cache options = {}
|
11
|
+
BarkingIguana::CacheBucket.get self.class.name, options
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
def class_cache options = {}
|
17
|
+
BarkingIguana::CacheBucket.get name, options
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: barking_iguana-cache_bucket
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Craig R Webster
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: null_logger
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: Naive in-memory cache with expiry
|
70
|
+
email:
|
71
|
+
- craig@barkingiguana.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- barking_iguana-cache_bucket.gemspec
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/barking_iguana/cache_bucket.rb
|
86
|
+
- lib/barking_iguana/cache_bucket/cache_bucket.rb
|
87
|
+
- lib/barking_iguana/cache_bucket/dsl.rb
|
88
|
+
- lib/barking_iguana/cache_bucket/version.rb
|
89
|
+
homepage: https://github.com/barkingiguana/cache_bucket
|
90
|
+
licenses: []
|
91
|
+
metadata:
|
92
|
+
allowed_push_host: https://rubygems.org
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.4.5.1
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Naive in-memory cache with expiry
|
113
|
+
test_files: []
|