cache_local 0.0.1

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: 9274375b604185a3d1fede11b614ee6cd4feda28
4
+ data.tar.gz: 0c3436e9de8dc6391452d278e25454072286e758
5
+ SHA512:
6
+ metadata.gz: 0b194d3f95f0f09c50dd506f94d9eeeb22001aa798ca678a463dfb82e25a159dc891afe1c68a61f864c77a5d55416ee630af0277fd27d769bfd564d4c2c963fe
7
+ data.tar.gz: 0ff5920d7a37b4da28542fb8a7300f76ec39cf029fccbbda4bc8199c1f440f0cd43bb0dd31574f9f08e3f480c12e64e5f852655a6b3a7ddfc3045c945d3c0ba3
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENCE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Kei Tsuchiya
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # cache_local
2
+ [![Gem Version](https://badge.fury.io/rb/cache_local.png)](http://badge.fury.io/rb/cache_local)
3
+ CacheLocal provides very simple cache function. There are two types of chache: file cache and process cache.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cache_local'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cache_local
18
+
19
+ ## Usage
20
+ ### File Cache
21
+ ```ruby
22
+ require 'cache_local'
23
+
24
+ file_cache = CacheLocal::File.new(path_to_cache_directory, namespace)
25
+ file_cache.set(key, value)
26
+ file_cache.get(key
27
+ file_cache.delete(key)
28
+ file_cache.set(key, value, expiration_time) # enable to specify expiration time [sec]
29
+ ```
30
+
31
+ ### Process Cache
32
+ ```ruby
33
+ require 'cache_local'
34
+
35
+ process_cache = CacheLocal::Process.new(namespace)
36
+ process_cache.set(key, value)
37
+ process_cache.get(key)
38
+ process_cache.delete(key)
39
+ process_cache.set(key, value, expiration_time) # enable to specify expiration time [sec]
40
+ ```
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,22 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'cache_local'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'cache_local'
7
+ spec.version = CacheLocal::VERSION
8
+ spec.authors = ['Kei Tsuchiya']
9
+ spec.email = ['kei.tsuchiya86@gmail.com']
10
+ spec.description = %q{Simple local cache}
11
+ spec.summary = %q{Enable to cache using file or process}
12
+ spec.homepage = 'https://github.com/kei500/cache_local'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'rake'
21
+ spec.add_development_dependency 'rspec', '~> 3'
22
+ end
@@ -0,0 +1,6 @@
1
+ require 'cache_local/file'
2
+ require 'cache_local/process'
3
+
4
+ module CacheLocal
5
+ VERSION = '0.0.1'
6
+ end
@@ -0,0 +1,45 @@
1
+ require 'fileutils'
2
+
3
+ module CacheLocal
4
+ class File
5
+ def initialize(path, namespace = nil)
6
+ @path = path
7
+ @namespace = namespace
8
+ if namespace
9
+ FileUtils.mkdir_p(::File.join(path, namespace)) unless FileTest.exist?(::File.join(path, namespace))
10
+ end
11
+ end
12
+
13
+ def set(key, value, expiration_time = 0)
14
+ file = ::File.open(filename(key), 'w')
15
+ Marshal.dump({value: value, expiration_time: expiration_time}, file)
16
+ file.close
17
+ end
18
+
19
+ def get(key)
20
+ return nil unless ::File.exists?(filename(key))
21
+
22
+ file = ::File.open(filename(key), 'r')
23
+ cache = Marshal.load(file)
24
+ is_expired = cache[:expiration_time] != 0 && Time.now - file.mtime > cache[:expiration_time]
25
+ file.close
26
+
27
+ if is_expired
28
+ delete(key)
29
+ return nil
30
+ end
31
+
32
+ cache[:value]
33
+ end
34
+
35
+ def delete(key)
36
+ ::File.delete(filename(key)) if ::File.exists?(filename(key))
37
+ end
38
+
39
+ private
40
+
41
+ def filename(key)
42
+ @namespace ? ::File.join(@path, @namespace, key) : ::File.join(@path, key)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,34 @@
1
+ module CacheLocal
2
+ class Process
3
+ def initialize(namespace = nil)
4
+ @namespace = namespace
5
+ @cache = {@namespace => {}}
6
+ end
7
+
8
+ def set(key, value, expiration_time = 0)
9
+ @cache[@namespace][key] = {value: value, expiration_time: expiration_time, set_time: Time.now}
10
+ end
11
+
12
+ def get(key)
13
+ return nil unless @cache[@namespace][key]
14
+
15
+ if expired?(key)
16
+ delete(key)
17
+ return nil
18
+ end
19
+
20
+ @cache[@namespace][key][:value]
21
+ end
22
+
23
+ def delete(key)
24
+ @cache[@namespace].delete(key)
25
+ end
26
+
27
+ private
28
+
29
+ def expired?(key)
30
+ return false if @cache[@namespace][key][:expiration_time] == 0
31
+ Time.now - @cache[@namespace][key][:set_time] > @cache[@namespace][key][:expiration_time]
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,48 @@
1
+ $: << File.expand_path(File.join(__FILE__, '..', '..', '..', 'lib'))
2
+ require 'cache_local'
3
+
4
+ describe CacheLocal::File do
5
+ before do
6
+ @cache = CacheLocal::File.new(File.join('', 'tmp'), 'cache_local')
7
+ end
8
+
9
+ describe '#get' do
10
+ describe "when expiration_time isn't set" do
11
+ before do
12
+ @cache.set('key', 'value')
13
+ end
14
+ it "returns 'value' when key exists" do
15
+ expect(@cache.get('key')).to eq('value')
16
+ end
17
+
18
+ it "returns nil when key dosen't exist" do
19
+ expect(@cache.get('hoge')).to eq(nil)
20
+ end
21
+ end
22
+
23
+ describe "when expiration_time is set" do
24
+ before do
25
+ @cache.set('key', 'value', 1)
26
+ end
27
+ it "returns 'value' when key isn't expired" do
28
+ expect(@cache.get('key')).to eq('value')
29
+ end
30
+
31
+ it "returns nil when key is expired" do
32
+ sleep(1)
33
+ expect(@cache.get('key')).to eq(nil)
34
+ end
35
+ end
36
+ end
37
+
38
+ describe '#delete' do
39
+ before do
40
+ @cache.set('key', 'value')
41
+ end
42
+
43
+ it 'returns nil when key is deleted' do
44
+ @cache.delete('key')
45
+ expect(@cache.get('hoge')).to eq(nil)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,48 @@
1
+ $: << File.expand_path(File.join(__FILE__, '..', '..', '..', 'lib'))
2
+ require 'cache_local'
3
+
4
+ describe CacheLocal::Process do
5
+ before do
6
+ @cache = CacheLocal::Process.new('cache_local')
7
+ end
8
+
9
+ describe '#get' do
10
+ describe "when expiration_time isn't set" do
11
+ before do
12
+ @cache.set('key', 'value')
13
+ end
14
+ it "returns 'value' when key exists" do
15
+ expect(@cache.get('key')).to eq('value')
16
+ end
17
+
18
+ it "returns nil when key dosen't exist" do
19
+ expect(@cache.get('hoge')).to eq(nil)
20
+ end
21
+ end
22
+
23
+ describe "when expiration_time is set" do
24
+ before do
25
+ @cache.set('key', 'value', 1)
26
+ end
27
+ it "returns 'value' when key isn't expired" do
28
+ expect(@cache.get('key')).to eq('value')
29
+ end
30
+
31
+ it "returns nil when key is expired" do
32
+ sleep(1)
33
+ expect(@cache.get('key')).to eq(nil)
34
+ end
35
+ end
36
+ end
37
+
38
+ describe '#delete' do
39
+ before do
40
+ @cache.set('key', 'value')
41
+ end
42
+
43
+ it 'returns nil when key is deleted' do
44
+ @cache.delete('key')
45
+ expect(@cache.get('hoge')).to eq(nil)
46
+ end
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cache_local
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kei Tsuchiya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3'
41
+ description: Simple local cache
42
+ email:
43
+ - kei.tsuchiya86@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENCE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - cache_local.gemspec
54
+ - lib/cache_local.rb
55
+ - lib/cache_local/file.rb
56
+ - lib/cache_local/process.rb
57
+ - spec/cache_local/file_spec.rb
58
+ - spec/cache_local/process_spec.rb
59
+ homepage: https://github.com/kei500/cache_local
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Enable to cache using file or process
83
+ test_files:
84
+ - spec/cache_local/file_spec.rb
85
+ - spec/cache_local/process_spec.rb