cache_store 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
+ SHA1:
3
+ metadata.gz: 06d887fb2a9be3c965f80c4abcf44b20880ec695
4
+ data.tar.gz: b28e631bb46f71a12585691c737993392d7e9dd9
5
+ SHA512:
6
+ metadata.gz: 0c22390d96e6d7fedd435ef3dadd7fefa9e7c1c5f78a0690a9ce97f28aa7cc0e31ca881fbcfe517eb35b1096606842b7532c1a1f4b5bba7e12316c789b34aa2a
7
+ data.tar.gz: 81a277cb0784b3e17b39d88f43dcd22ece0cfab5b48bd679bf9bc98c42c3663c02185f33fa4deff1fc54d6a5a22f385ecff2e95e61e45a4a1b0b78162f74fbfd
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at vaughan.britton@sage.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cache_store.gemspec
4
+ gemspec
5
+
6
+ gem 'pry'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 vaughanbrittonsage
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,156 @@
1
+ # CacheStore
2
+
3
+ Welcome to CacheStore!
4
+
5
+ This is the base for a cache framework that includes a basic in memory cache store, along with a dependency contract for additional provider implementations plugins.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'cache_store'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cache_store
22
+
23
+ ## Implementations
24
+
25
+ All cache store implementations adhere to the following contract:
26
+
27
+
28
+ class CacheStoreContract
29
+
30
+ def set(key, value, expires_in = 0)
31
+
32
+ end
33
+
34
+ def get(key, expires_in = 0, &block)
35
+
36
+ end
37
+
38
+ def remove(key)
39
+
40
+ end
41
+
42
+ def exist?(key)
43
+
44
+ end
45
+ end
46
+
47
+
48
+ **#set**
49
+
50
+ This method is called to store a value in the cache store for a unique key.
51
+
52
+ Params:
53
+
54
+ - **key** [String]
55
+ This is the unique key to reference the value being set within this cache store
56
+ - **value** [Object]
57
+ This is the value to set within this cache store.
58
+ - **expires_in** [Integer] [Optional]
59
+ This is the number of seconds from the current time that this value should expire.
60
+
61
+ Example:
62
+
63
+ #set with expires_in specified
64
+ cache_store.set('country_code', 'en-GB', 180)
65
+
66
+
67
+ > The above example will store the **value** 'en-GB' under the **key**
68
+ > 'country_code' for **expiry** time of 180 seconds (2minutes).
69
+ > Any requests to the cache_store for the 'country_code' key within the next 180 seconds (2minutes) will return the 'en-GB' value.
70
+ > Requests for the key after the expiry time will return **nil** if no hydration block has been specified in the request.
71
+ > If you don't specify an **expires_in** parameter then the value stored will not expire for the lifespan of the cache_store.
72
+
73
+ **#get**
74
+
75
+ This method is called to request a value from the cache store for a unique key.
76
+
77
+ Params:
78
+
79
+ - **key** [String]
80
+ This is the unique key of the value you want to fetch from within the cache store.
81
+ - **expires_in** [Integer] [Optional]
82
+ This is the number of seconds from the current time that this value should expire.
83
+
84
+ > (This is used in conjunction with the hydrate block to populate the cache key if it is empty.)
85
+
86
+ - **&block** [Block] [Optional]
87
+ This is the hydration block that when specified is used to populate the cache_store with the value for the specified key.
88
+
89
+ Examples:
90
+
91
+ #example without a hydration block
92
+ value = cache_store.get('country_code')
93
+
94
+ > This would return the **value** stored for the 'country_code' key or **nil** if the value had expired or was not found.
95
+
96
+ #example with a hydration block
97
+ value = cache_store.get('country_code', 180) do
98
+ {
99
+ return 'en-GB'
100
+ }
101
+
102
+ > This would execute the hydration block if the value was not found for the specified key or if the value had expired.
103
+
104
+ **#remove**
105
+
106
+ This method is called to remove a value from the cache store by it's unique key.
107
+
108
+ Params:
109
+
110
+ - **key** [String] This is the unique key of the item to remove from the cache store.
111
+
112
+ Example:
113
+
114
+ cache_store.remove('country_code')
115
+
116
+
117
+ **#exist?**
118
+
119
+ This method is called to check if a value has been stored in the cache store for a specific key.
120
+
121
+ Params:
122
+
123
+ **key** [String]
124
+ This is the unique key of the value to check for.
125
+
126
+ Example:
127
+
128
+ if cache_store.exist?('country_code')
129
+ ....do logic here
130
+ end
131
+
132
+
133
+ ##LocalCacheStore
134
+
135
+ The local cache store is a ruby in memory cache store that has no dependency on rails or any other frameworks. Multiple instances of the cache store can be created as required to maintain isolated cache stores, which are perfect for development and testing when your production application cache uses reds or memcached etc as a distributed cache.
136
+
137
+ #create a new instance of the cache store
138
+ cache_store = LocalCacheStore.new
139
+
140
+ > **CacheStore** works perfectly with **Sinject** a dependency injection framework allowing you to switch the cache store implementations used for different environments.
141
+
142
+ ## Development
143
+
144
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
145
+
146
+ 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).
147
+
148
+ ## Contributing
149
+
150
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vaughanbrittonsage/cache_store. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
151
+
152
+
153
+ ## License
154
+
155
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
156
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cache_store"
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,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,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cache_store/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cache_store"
8
+ spec.version = CacheStore::VERSION
9
+ spec.authors = ["vaughanbrittonsage"]
10
+ spec.email = ["vaughan.britton@sage.com"]
11
+
12
+ spec.summary = 'This is the base for a cache framework that includes a basic in memory cache store, along with a dependency contract for additional provider implementations plugins.'
13
+ spec.description = 'This is the base for a cache framework that includes a basic in memory cache store, along with a dependency contract for additional provider implementations plugins.'
14
+ spec.homepage = "https://github.com/vaughanbrittonsage/cache_store"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
@@ -0,0 +1,123 @@
1
+ require "cache_store/version"
2
+ require 'date'
3
+ require 'pry'
4
+ #This class is used to define the contract that CacheStore implementations must adhere to.
5
+ class CacheStoreContract
6
+
7
+ def initialize(namespace = '')
8
+ @namespace = namespace
9
+ end
10
+
11
+ #This method is called to set a value within this cache store by it's key.
12
+ #
13
+ # @param [String] This is the unique key to reference the value being set within this cache store.
14
+ # @param [Object] This is the value to set within this cache store.
15
+ # @param [Integer] This is the number of seconds from the current time that this value should expire.
16
+ def set(key, value, expires_in = 0)
17
+
18
+ end
19
+
20
+ #This method is called to get a value from this cache store by it's unique key.
21
+ #
22
+ # @param [String] This is the unique key to reference the value to fetch from within this cache store.
23
+ # @param [Integer] This is the number of seconds from the current time that this value should expire. (This is used in conjunction with the block to hydrate the cache key if it is empty.)
24
+ # @param [Block] This block is provided to hydrate this cache store with the value for the request key when it is not found.
25
+ def get(key, expires_in = 0, &block)
26
+
27
+ end
28
+
29
+ # This method is called to remove a value from this cache store by it's unique key.
30
+ #
31
+ # @param [String] This is the unique key to reference the value to remove from this cache store.
32
+ def remove(key)
33
+
34
+ end
35
+
36
+ # This method is called to check if a value exists within this cache store for a specific key.
37
+ #
38
+ # @param [String] This is the unique key to reference the value to check for within this cache store.
39
+ def exist?(key)
40
+
41
+ end
42
+ end
43
+
44
+ #This class is used to implement a local in memory cache store.
45
+ class LocalCacheStore
46
+
47
+ attr_accessor :store
48
+
49
+ def initialize(namespace = '')
50
+ @store = Array.new
51
+ @namespace = namespace
52
+ end
53
+
54
+ #This method is called to set a value within this cache store by it's key.
55
+ #
56
+ # @param [String] This is the unique key to reference the value being set within this cache store.
57
+ # @param [Object] This is the value to set within this cache store.
58
+ # @param [Integer] This is the number of seconds from the current time that this value should expire.
59
+ def set(key, value, expires_in = 0)
60
+ remove(build_key(key))
61
+ expires = nil
62
+ if expires_in > 0
63
+ now = DateTime.now
64
+ expires = DateTime.new(now.year, now.month, now.day, 0, 0, expires_in)
65
+ end
66
+ @store.push({ key: build_key(key), value: value, expires: expires})
67
+ end
68
+
69
+ #This method is called to get a value from this cache store by it's unique key.
70
+ #
71
+ # @param [String] This is the unique key to reference the value to fetch from within this cache store.
72
+ # @param [Integer] This is the number of seconds from the current time that this value should expire. (This is used in conjunction with the block to hydrate the cache key if it is empty.)
73
+ # @param [Block] This block is provided to hydrate this cache store with the value for the request key when it is not found.
74
+ def get(key, expires_in = 0, &block)
75
+
76
+ #look for the cache item in the store
77
+ items = @store.select { |i| i[:key] == build_key(key) }
78
+ item = if !items.empty? then items[0] else nil end
79
+ #check if a valid item was found in the store
80
+ if item == nil || (item[:expires] != nil && item[:expires] <= DateTime.now)
81
+ #a valid item wasn't found so check if a hydration block was specified.
82
+ if block_given?
83
+ #create the item from the block
84
+ value = yield
85
+ #put the item in the store
86
+ set(build_key(key), value, expires_in)
87
+ return value
88
+ else
89
+ #no hydration block was specified
90
+
91
+ #check if an expired item was found
92
+ if item != nil
93
+ #remove the expired item from the store
94
+ remove(build_key(key))
95
+ end
96
+ return nil
97
+ end
98
+ end
99
+
100
+ #return the item
101
+ return item[:value]
102
+ end
103
+
104
+ # This method is called to remove a value from this cache store by it's unique key.
105
+ #
106
+ # @param [String] This is the unique key to reference the value to remove from this cache store.
107
+ def remove(key)
108
+ @store.delete_if { |i| i[:key] == build_key(key) }
109
+ end
110
+
111
+ # This method is called to check if a value exists within this cache store for a specific key.
112
+ #
113
+ # @param [String] This is the unique key to reference the value to check for within this cache store.
114
+ def exist?(key)
115
+ !@store.select { |i| i[:key] == build_key(key) }.empty?
116
+ end
117
+
118
+ private
119
+
120
+ def build_key(key)
121
+ @namespace + key.to_s
122
+ end
123
+ end
@@ -0,0 +1,3 @@
1
+ module CacheStore
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cache_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - vaughanbrittonsage
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-22 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: This is the base for a cache framework that includes a basic in memory
56
+ cache store, along with a dependency contract for additional provider implementations
57
+ plugins.
58
+ email:
59
+ - vaughan.britton@sage.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - cache_store.gemspec
74
+ - lib/cache_store.rb
75
+ - lib/cache_store/version.rb
76
+ homepage: https://github.com/vaughanbrittonsage/cache_store
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.5.1
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: This is the base for a cache framework that includes a basic in memory cache
100
+ store, along with a dependency contract for additional provider implementations
101
+ plugins.
102
+ test_files: []
103
+ has_rdoc: