emcache 0.1.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: 4a98b657fc6dda73b21bab46c51c967aef98d946
4
+ data.tar.gz: 00807ba57cbb0944c9e85f1477d15c52df9e58a0
5
+ SHA512:
6
+ metadata.gz: 9daa2231af1facde686e3d677096167edce5523a0096bbf18d3e4d161286fcb370993986c0c80ed5557148cd4454ec1cab928366f68d190196e4ec6f8afae9c8
7
+ data.tar.gz: cb8db1106e362ba5b76a910b9c852be843c3ddc71ee479c2484416ed1730eeeaf33d0be76f290ccd107549bf33dc4d2f8940b75614a8a4372a028bdf55cf2771
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in emcache.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ emcache (0.1.0)
5
+ connection_pool
6
+ redis
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.2.0)
12
+ connection_pool (2.2.0)
13
+ parser (2.3.0.2)
14
+ ast (~> 2.2)
15
+ powerpack (0.1.1)
16
+ rainbow (2.1.0)
17
+ rake (10.5.0)
18
+ redis (3.2.2)
19
+ rubocop (0.36.0)
20
+ parser (>= 2.3.0.0, < 3.0)
21
+ powerpack (~> 0.1)
22
+ rainbow (>= 1.99.1, < 3.0)
23
+ ruby-progressbar (~> 1.7)
24
+ ruby-progressbar (1.7.5)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (~> 1.10)
31
+ emcache!
32
+ rake (~> 10.0)
33
+ rubocop (~> 0.36)
34
+
35
+ BUNDLED WITH
36
+ 1.10.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Emjay Kim
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,39 @@
1
+ # Emcache
2
+
3
+ Emcache: transparent cache
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'emcache'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install emcache
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ 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.
28
+
29
+ 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).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mjkim/emcache-rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
39
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "emcache"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/emcache.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'emcache/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "emcache"
8
+ spec.version = Emcache::VERSION
9
+ spec.authors = ["Myungjun Kim"]
10
+ spec.email = ["niduss@gmail.com"]
11
+
12
+ spec.summary = %q{Emcache: transparent redis cache}
13
+ spec.description = %q{Emcache}
14
+ spec.homepage = "https://github.com/mjkim/emcache-rb"
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.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rubocop", "~> 0.36"
25
+ spec.add_runtime_dependency "redis", "~> 3.2"
26
+ spec.add_runtime_dependency "connection_pool", "~> 2.2"
27
+ end
data/lib/emcache.rb ADDED
@@ -0,0 +1,100 @@
1
+ require 'emcache/version'
2
+ require 'connection_pool'
3
+ require 'ostruct'
4
+ require 'digest/sha1'
5
+ require 'redis'
6
+ require 'logger'
7
+
8
+ class Emcache
9
+ attr_reader :redis, :prefix, :ttl, :timeout
10
+
11
+ STATUS_SUCCESS = 0
12
+ STATUS_NOT_EXIST = 1
13
+ STATUS_LOCKED = 2
14
+
15
+ def initialize(options = {})
16
+ parse_option(options.dup)
17
+
18
+ @lua = {
19
+ get: scripts('base', 'get'),
20
+ set: scripts('base', 'set'),
21
+ del: scripts('base', 'del')
22
+ }
23
+ end
24
+
25
+ def call(method, keys, args)
26
+ logger.debug("method: #{method} sha: #{@lua[method].sha}"\
27
+ " key: #{keys.inspect} args: #{args.inspect}")
28
+ redis.with do |conn|
29
+ begin
30
+ ret = conn.evalsha(@lua[method].sha, keys, args)
31
+ logger.debug("method: #{method} return: #{ret}")
32
+ ret
33
+ rescue Redis::CommandError => ex
34
+ logger.debug("exception: #{ex.inspect}")
35
+ raise ex unless ex.message.start_with? 'NOSCRIPT '
36
+
37
+ load_script(conn, method)
38
+ retry
39
+ end
40
+ end
41
+ end
42
+
43
+ def get(key)
44
+ status, value = call :get, [key], [prefix, block_given? ? timeout : 0]
45
+ if block_given? && status != STATUS_SUCCESS
46
+ lock = value
47
+ value = yield(key)
48
+
49
+ _set(key, lock, value) if status == STATUS_NOT_EXIST
50
+ end
51
+ value
52
+ end
53
+
54
+ def set(key, value)
55
+ _set(key, '*', value)
56
+ end
57
+
58
+ def del(key)
59
+ call :del, [key], [prefix]
60
+ end
61
+
62
+ private
63
+
64
+ attr_reader :logger
65
+
66
+ def _set(key, lock, value)
67
+ call :set, [key, value], [prefix, lock, ttl]
68
+ end
69
+
70
+ def load_script(conn, method)
71
+ logger.debug("load script: #{method}")
72
+ server_sha = conn.script('load', @lua[method].code)
73
+ fail('sha mismatch') if server_sha != @lua[method].sha
74
+ end
75
+
76
+ def script(name)
77
+ File.read("#{File.dirname(__FILE__)}/../scripts/#{name}.lua")
78
+ end
79
+
80
+ def scripts(*names)
81
+ code = names.map { |name| script(name) }.join("\n")
82
+ sha = Digest::SHA1.hexdigest(code)
83
+ OpenStruct.new code: code, sha: sha
84
+ end
85
+
86
+ def parse_option(options)
87
+ @prefix = options.fetch(:prefix, 'DEFAULT')
88
+ @timeout = options.fetch(:timeout, 100)
89
+ @ttl = options.fetch(:ttl, 0)
90
+ @logger = Logger.new(STDOUT)
91
+ @logger.level = options.fetch(:log_level, Logger::WARN)
92
+
93
+ cpo_opts = { size: 5, timeout: 5 }
94
+ cpo_opts.merge!(options.fetch(:connection_pool, {}))
95
+
96
+ redis_opts = {}
97
+ redis_opts.merge!(options.fetch(:redis, {}))
98
+ @redis = ConnectionPool.new(cpo_opts) { Redis.new(redis_opts) }
99
+ end
100
+ end
@@ -0,0 +1,3 @@
1
+ class Emcache
2
+ VERSION = "0.1.1"
3
+ end
data/scripts/base.lua ADDED
@@ -0,0 +1,12 @@
1
+ local arg_count = table.getn(ARGV)
2
+
3
+ local prefix_name = 'DEFAULT'
4
+
5
+ if arg_count >= 1 then
6
+ prefix_name = ARGV[1]
7
+ end
8
+
9
+ local key = prefix_name..'::{'..KEYS[1]..'}'
10
+ local value = KEYS[2]
11
+
12
+ local lock_key = key..'::'..'LOCK'
data/scripts/del.lua ADDED
@@ -0,0 +1,2 @@
1
+ redis.call('DEL', lock_key)
2
+ redis.call('DEL', key)
data/scripts/get.lua ADDED
@@ -0,0 +1,22 @@
1
+ local timeout_ms = 10
2
+ if arg_count >= 2 then
3
+ timeout_ms = tonumber(ARGV[2])
4
+ end
5
+
6
+ local ret = redis.call('GET', key)
7
+
8
+ if ret == false then
9
+ if timeout_ms == 0 then
10
+ return {1, nil}
11
+ end
12
+
13
+ local locked = redis.call('GET', lock_key)
14
+ if locked then
15
+ return {2, tonumber(locked)}
16
+ else
17
+ local lock_value = redis.call('INCR', 'COUNTER')
18
+ redis.call('SET',lock_key, lock_value, 'PX', timeout_ms)
19
+ return {1, lock_value}
20
+ end
21
+ end
22
+ return {0, ret}
data/scripts/set.lua ADDED
@@ -0,0 +1,24 @@
1
+ local force = (ARGV[2] == '*')
2
+ local lock_value = '*'
3
+
4
+ if not force then
5
+ lock_value = redis.call('GET', lock_key)
6
+ end
7
+
8
+ local ttl = 0
9
+ if arg_count >= 3 then
10
+ ttl = tonumber(ARGV[3])
11
+ end
12
+
13
+ if lock_value ~= ARGV[2] then
14
+ return {2, tonumber(lock_value)}
15
+ end
16
+
17
+ redis.call('SET', key, value)
18
+ redis.call('DEL', lock_key)
19
+
20
+ if ttl > 0 then
21
+ redis.call('PEXPIRE', key, ttl)
22
+ end
23
+
24
+ return {0, 1}
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: emcache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Myungjun Kim
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-14 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.36'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.36'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: connection_pool
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.2'
83
+ description: Emcache
84
+ email:
85
+ - niduss@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - CODE_OF_CONDUCT.md
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - emcache.gemspec
99
+ - lib/emcache.rb
100
+ - lib/emcache/version.rb
101
+ - scripts/base.lua
102
+ - scripts/del.lua
103
+ - scripts/get.lua
104
+ - scripts/set.lua
105
+ homepage: https://github.com/mjkim/emcache-rb
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.4.6
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: 'Emcache: transparent redis cache'
129
+ test_files: []