ruby_cream 0.1.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
+ SHA256:
3
+ metadata.gz: 21e2768f002dc10a42ecb043e0a64a01c53a96155c415ef1281ac9e0f46f9c5b
4
+ data.tar.gz: 5414ed8530469c7a891c0b60012e132bb27fc6aaaaa8f07030729181c4e9a9cf
5
+ SHA512:
6
+ metadata.gz: f979f0cf4ebd13ba9fdeb0bf9bb65a440f646fd1abbbd86eb2fa4b5a3ca1f42daa79eefca170e01878050117da852abd760bc97f2f6c71be3862d73a735cd468
7
+ data.tar.gz: 39495e30487be5c5a118fdaa8109fc9b7bd7dd4990123e0b073cc4f29956c49a9c9b1ae7da0a491d0e1503bcb7b84decd61d608e72040f820a80e99d5a2a7a0a
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cream.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby_cream (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.11.3)
10
+ rake (10.5.0)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler (~> 1.17.3)
17
+ minitest (~> 5.11.3)
18
+ rake (~> 10.0)
19
+ ruby_cream!
20
+
21
+ BUNDLED WITH
22
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Jonathan Thom
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,68 @@
1
+ # C.R.E.A.M.
2
+
3
+ Cache Rules Everything Around Me.
4
+
5
+ Simple in-memory Ruby cache. Inspired by [go-cache](https://github.com/patrickmn/go-cache).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem "ruby_cream"
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cream
22
+
23
+
24
+ ## Usage
25
+
26
+ ```
27
+ # Create a new cache instance with item duration in seconds as the argument:
28
+ cache = Cream::Cache.new(10)
29
+
30
+ # Set a cache value:
31
+ cache.set(:test, "result")
32
+
33
+ # Get a cache value:
34
+ cache.get(:test)
35
+ => "result"
36
+
37
+ # After some time as has passed, the item will expire:
38
+ cache.get(:test)
39
+ => nil
40
+
41
+ # Reset a key. Time to expiration will reset.
42
+ cache.set(:test, "original")
43
+ cache.set(:test, "reset")
44
+ cache.get(:test)
45
+ => "reset"
46
+
47
+ # Cache#items and Cache#duration_seconds are both publicly readable for debugging.
48
+ cache.items
49
+ => {:test=>#<Cream::CacheItem:0x00007ffac103f4d0 @value="result", @expires_at=2019-02-07 08:24:57 -0800>}
50
+
51
+ cache.duration_seconds
52
+ => 10
53
+
54
+ ```
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ 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)
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/JonathanWThom/cream.
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "rake/testtask"
2
+ require "bundler/gem_tasks"
3
+
4
+ task default: "test"
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "test"
8
+ t.pattern = "test/*_test.rb"
9
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cream"
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__)
data/bin/setup ADDED
@@ -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,29 @@
1
+ require "cream/cache_item"
2
+
3
+ module Cream
4
+ class Cache
5
+ attr_reader :duration_seconds, :items
6
+
7
+ def initialize(duration_seconds)
8
+ @duration_seconds = duration_seconds
9
+ @items = {}
10
+ end
11
+
12
+ def set(key, value)
13
+ expires_at = Time.now + duration_seconds
14
+ item = Cream::CacheItem.new(value, expires_at)
15
+ items.merge!(Hash[key, item])
16
+ end
17
+
18
+ def get(key)
19
+ item = items[key]
20
+
21
+ if item&.expired?
22
+ items.delete(key)
23
+ return nil
24
+ end
25
+
26
+ item&.value
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ module Cream
2
+ class CacheItem
3
+ attr_reader :value, :expires_at
4
+
5
+ def initialize(value, expires_at)
6
+ @value = value
7
+ @expires_at = expires_at
8
+ end
9
+
10
+ def expired?
11
+ Time.now > expires_at
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Cream
2
+ VERSION = "0.1.0"
3
+ end
data/lib/cream.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "cream/version"
2
+ require "cream/cache"
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "cream/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ruby_cream"
7
+ spec.version = Cream::VERSION
8
+ spec.authors = ["Jonathan Thom"]
9
+ spec.email = ["jonathan.thom1990@gmail.com"]
10
+ spec.summary = "Simple in-memory Ruby cache"
11
+ spec.homepage = "https://github.com/JonathanWThom/cream"
12
+ spec.license = "MIT"
13
+
14
+ if spec.respond_to?(:metadata)
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ else
19
+ raise "RubyGems 2.0 or newer is required to protect against " \
20
+ "public gem pushes."
21
+ end
22
+
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.17.3"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "minitest", "~> 5.11.3"
33
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_cream
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Thom
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-08 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.3
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.3
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.11.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.11.3
55
+ description:
56
+ email:
57
+ - jonathan.thom1990@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/cream.rb
71
+ - lib/cream/cache.rb
72
+ - lib/cream/cache_item.rb
73
+ - lib/cream/version.rb
74
+ - ruby_cream.gemspec
75
+ homepage: https://github.com/JonathanWThom/cream
76
+ licenses:
77
+ - MIT
78
+ metadata:
79
+ allowed_push_host: https://rubygems.org/
80
+ homepage_uri: https://github.com/JonathanWThom/cream
81
+ source_code_uri: https://github.com/JonathanWThom/cream
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.0.1
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Simple in-memory Ruby cache
101
+ test_files: []