kv_cache 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8ba53100a2cf7afe3647d85d91eb49ea1cfa6102
4
+ data.tar.gz: ebb121477c37edf9bf0b2f976f7320c3d093403b
5
+ SHA512:
6
+ metadata.gz: f27f5d3bde4ccf1255943ba2a4fb3a6c3b1fa24ed9e0c930d001841a0a70bf08a46f07dbc207b9b29b9afe4d26008f668cd4aa21fb020755485f4f2f221071f6
7
+ data.tar.gz: b079019e07e33a2e3e2a749a31011ab48baa5a538b0579f704ff7bcd3798a7390228fee4fe2d4dd479b4f441e1697db3693c972a3f88e3b3a7aa7825789e84f8
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kv_cache.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 azhao1981
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,56 @@
1
+ # KvCache
2
+
3
+ kv_cache is a tool caching keys and values in a ruby project.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'kv_cache'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install kv_cache
19
+
20
+ ## Usage
21
+
22
+ ```
23
+ require 'kv_cache'
24
+
25
+ class City < ActiveRecord::Base
26
+ attr_accessible :name, :province
27
+ include KvCache
28
+
29
+ # kv_cache :method_name, expire_time, :key, values_lambda
30
+ scope :guangxi, ->{ puts "fecth from db"
31
+ Store.call {where(province: "guangxi")} }
32
+
33
+ after_save :kv_cache_reset
34
+
35
+ def kv_cache_reset
36
+ Store.delete(self.province)
37
+ end
38
+ end
39
+
40
+ # some code
41
+ City.guangxi # first call will :
42
+ # fecth from db
43
+ # select * from cities where province = 'guangxi'
44
+ cs = City.guangxi # will just return result above
45
+ cs.first.save # will kv_cache_reset("guangxi")
46
+ City.guangxi # fecth from db again
47
+
48
+ ```
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/kv_cache.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kv_cache/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kv_cache"
8
+ spec.version = KvCache::VERSION
9
+ spec.authors = ["azhao1981"]
10
+ spec.email = ["azhao.1981@gmail.com"]
11
+ spec.description = %q{Since using kv_cache, Mami need't to warry my db crashing anymore. :)}
12
+ spec.summary = %q{cache your data that you don't need to fetch from db.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "dalli"
24
+ end
@@ -0,0 +1,42 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "dalli"
3
+
4
+ module KvCache
5
+ class Store
6
+ class << self
7
+ # call yield if key is not exist
8
+ def call(key,expire_time)
9
+ res = storer.get(key)
10
+ return res if res
11
+
12
+ res = yield
13
+
14
+ storer.set(key, res, expire_time)
15
+
16
+ res
17
+ end
18
+
19
+ # delete key
20
+ def delete(*keys)
21
+ keys.each { |e| storer.delete(e) }
22
+ end
23
+
24
+ # like Dalli for memcached
25
+ # #get, #set is Necessary!!
26
+ def storer
27
+ @@_storer ||= defalt_storer
28
+ end
29
+
30
+ def defalt_storer
31
+ opts = { :namespace => "app_v1", :compress => true }
32
+ Dalli::Client.new('127.0.0.1:11211', opts)
33
+ end
34
+
35
+ # opts = { :namespace => "app_v2", :compress => true }
36
+ # KvCache::Store.storer = Dalli::Client.new('127.0.0.1:11211', opts)
37
+ def storer=(_storer)
38
+ @@_storer = _storer
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module KvCache
2
+ VERSION = "0.0.1"
3
+ end
data/lib/kv_cache.rb ADDED
@@ -0,0 +1,6 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "kv_cache/version"
3
+ require "kv_cache/store"
4
+
5
+ module KvCache
6
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe "KvCache::Store" do
5
+ it "hello" do
6
+ KvCache::Store.new.should be_kind_of(KvCache::Store)
7
+ end
8
+
9
+ it "#call second time will get data from cache " do
10
+ call_time = 0
11
+ KvCache::Store.delete("hello")
12
+ res = KvCache::Store.call("hello", 20) do
13
+ call_time += 1
14
+ 90
15
+ end
16
+
17
+ call_time.should == 1
18
+ res.should == 90
19
+
20
+ res = KvCache::Store.call("hello", 20) do
21
+ call_time += 1
22
+ 90
23
+ end
24
+
25
+ call_time.should == 1
26
+ res.should == 90
27
+
28
+ KvCache::Store.delete("hello")
29
+
30
+ res = KvCache::Store.call("hello", 20) do
31
+ call_time += 1
32
+ 90
33
+ end
34
+
35
+ call_time.should == 2
36
+ res.should == 90
37
+ KvCache::Store.delete("hello")
38
+ end
39
+
40
+ it "#call will still exec after storer are changed " do
41
+ call_time = 0
42
+ KvCache::Store.delete("hello")
43
+ res = KvCache::Store.call("hello", 20) do
44
+ call_time += 1
45
+ 90
46
+ end
47
+
48
+ call_time.should == 1
49
+ res.should == 90
50
+
51
+ res = KvCache::Store.call("hello", 20) do
52
+ call_time += 1
53
+ 90
54
+ end
55
+
56
+ call_time.should == 1
57
+ res.should == 90
58
+
59
+ opts = { :namespace => "app_v2", :compress => true }
60
+ KvCache::Store.storer = Dalli::Client.new('127.0.0.1:11211', opts)
61
+
62
+ res = KvCache::Store.call("hello", 20) do
63
+ call_time += 1
64
+ 90
65
+ end
66
+
67
+ KvCache::Store.delete("hello")
68
+ call_time.should == 2
69
+ res.should == 90
70
+ KvCache::Store.delete("hello")
71
+ end
72
+ end
@@ -0,0 +1,19 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
18
+
19
+ require 'kv_cache'
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kv_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - azhao1981
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-20 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.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.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: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dalli
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Since using kv_cache, Mami need't to warry my db crashing anymore. :)
56
+ email:
57
+ - azhao.1981@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - kv_cache.gemspec
69
+ - lib/kv_cache.rb
70
+ - lib/kv_cache/store.rb
71
+ - lib/kv_cache/version.rb
72
+ - spec/kv_store/store_spec.rb
73
+ - spec/spec_helper.rb
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.1.11
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: cache your data that you don't need to fetch from db.
98
+ test_files:
99
+ - spec/kv_store/store_spec.rb
100
+ - spec/spec_helper.rb
101
+ has_rdoc: