dev_repository_hjq
huang 10 years ago
parent 6e7b294452
commit f898e2aa91

1
.gitignore vendored

@ -21,3 +21,4 @@
.rbenv-gemsets
.DS_Store
public/api_doc/
/.metadata

@ -0,0 +1,20 @@
<div id="tags_show">
<%= render :partial => "tags/tag_name",:locals => {:obj => obj,:non_list_all => false ,:object_flag => object_flag} %>
</div>
<% if User.current.logged? %>
<span> <%= toggle_link ("+ 添加标签"), 'put-tag-form', {:focus => 'tags_name'} %> </span>
<% end %>
<div id="put-tag-form" style="display: none">
<%= form_for "tag_for_save",:remote=>true,:url=>tag_path,
:update => "tags_show",
:complete => '$("#put-tag-form").slideUp();' do |f| %>
<%= f.text_field :name ,:id => "tags_name",:size=>"20",
:require=>true,
:maxlength => Setting.tags_max_length,
:minlength=>Setting.tags_min_length %>
<%= f.text_field :object_id,:value=> obj.id,:style=>"display:none"%>
<%= f.text_field :object_flag,:value=> object_flag,:style=>"display:none"%>
<a href="#" onclick='$("#tags_name").parent().submit();' type="button" class="submit f_l"></a>
<% end %>
</div>

@ -0,0 +1,13 @@
lib/dalli.rb b1fd9d39df06608fcae5bcf46e9940f95b186d22
lib/dalli/ring.rb e2cd42d8b963e669e2c8a83791fa56ec94f9ec55
lib/dalli/server.rb a42d734f9b3d654886c86f04fb4f0352e7147b1e
lib/dalli/client.rb 11afa0d702c68a151c87ea6e7ccdc863cf03884f
lib/dalli/socket.rb 18b7243332ec2dafa9a17c195944e321b684e67e
lib/dalli/options.rb d7ecb4c52b4ae2b222f319813234297e0951f82a
lib/dalli/version.rb fff3231b7f52d7fa1dabb78bf1f67dcef95c5378
lib/dalli/railtie.rb 63dc0fe85790a10225e867774f2c611d1c1ac46c
lib/dalli/compressor.rb 13b0cf3f607bd8bc9f969679b0b6e9dcb0a059d7
lib/dalli/cas/client.rb 983ded7ec738ed4502658150123e9c5ad7e3faa1
lib/rack/session/dalli.rb 2696ad72e8f9d7f5ceb232db0c8d9a8916192edb
lib/active_support/cache/dalli_store.rb 010d880e0f297d92b26c8f44e446add9d4fedfa2
lib/action_dispatch/middleware/session/dalli_store.rb 62236273ea28a91502871f31aa600e038358931a

@ -0,0 +1,12 @@
source 'https://rubygems.org'
gemspec
gem 'rake'
gem 'kgio', :platform => :mri
gem 'appraisal'
gem 'connection_pool'
group :test do
gem 'simplecov'
end

File diff suppressed because it is too large Load Diff

@ -0,0 +1,20 @@
Copyright (c) Mike Perham
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@ -0,0 +1,42 @@
Performance
====================
Caching is all about performance, so I carefully track Dalli performance to ensure no regressions.
You can optionally use kgio to give Dalli a 10-20% performance boost: `gem install kgio`.
Note I've added some benchmarks over time to Dalli that the other libraries don't necessarily have.
memcache-client
---------------
Testing 1.8.5 with ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]
user system total real
set:plain:memcache-client 1.860000 0.310000 2.170000 ( 2.188030)
set:ruby:memcache-client 1.830000 0.290000 2.120000 ( 2.130212)
get:plain:memcache-client 1.830000 0.340000 2.170000 ( 2.176156)
get:ruby:memcache-client 1.900000 0.330000 2.230000 ( 2.235045)
multiget:ruby:memcache-client 0.860000 0.120000 0.980000 ( 0.987348)
missing:ruby:memcache-client 1.630000 0.320000 1.950000 ( 1.954867)
mixed:ruby:memcache-client 3.690000 0.670000 4.360000 ( 4.364469)
dalli
-----
Testing with Rails 3.2.1
Using kgio socket IO
Testing 2.0.0 with ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.3.0]
user system total real
mixed:rails:dalli 1.580000 0.570000 2.150000 ( 3.008839)
set:plain:dalli 0.730000 0.300000 1.030000 ( 1.567098)
setq:plain:dalli 0.520000 0.120000 0.640000 ( 0.634402)
set:ruby:dalli 0.800000 0.300000 1.100000 ( 1.640348)
get:plain:dalli 0.840000 0.330000 1.170000 ( 1.668425)
get:ruby:dalli 0.850000 0.330000 1.180000 ( 1.665716)
multiget:ruby:dalli 0.700000 0.260000 0.960000 ( 0.965423)
missing:ruby:dalli 0.720000 0.320000 1.040000 ( 1.511720)
mixed:ruby:dalli 1.660000 0.640000 2.300000 ( 3.320743)
mixedq:ruby:dalli 1.630000 0.510000 2.140000 ( 2.629734)
incr:ruby:dalli 0.270000 0.100000 0.370000 ( 0.547618)

@ -0,0 +1,224 @@
Dalli [![Build Status](https://secure.travis-ci.org/mperham/dalli.png)](http://travis-ci.org/mperham/dalli) [![Dependency Status](https://gemnasium.com/mperham/dalli.png)](https://gemnasium.com/mperham/dalli) [![Code Climate](https://codeclimate.com/github/mperham/dalli.png)](https://codeclimate.com/github/mperham/dalli)
=====
Dalli is a high performance pure Ruby client for accessing memcached servers. It works with memcached 1.4+ only as it uses the newer binary protocol. It should be considered a replacement for the memcache-client gem.
The name is a variant of Salvador Dali for his famous painting [The Persistence of Memory](http://en.wikipedia.org/wiki/The_Persistence_of_Memory).
![Persistence of Memory](http://www.virtualdali.com/assets/paintings/31PersistenceOfMemory.jpg)
Dalli's initial development was sponsored by [CouchBase](http://www.couchbase.com/). Many thanks to them!
Design
------------
I decided to write Dalli after maintaining memcache-client for two years for a few specific reasons:
0. The code is mostly old and gross. The bulk of the code is a single 1000 line .rb file.
1. It has a lot of options that are infrequently used which complicate the codebase.
2. The implementation has no single point to attach monitoring hooks.
3. Uses the old text protocol, which hurts raw performance.
So a few notes. Dalli:
0. uses the exact same algorithm to choose a server so existing memcached clusters with TBs of data will work identically to memcache-client.
1. is approximately 20% faster than memcache-client (which itself was heavily optimized) in Ruby 1.9.2.
2. contains explicit "chokepoint" methods which handle all requests; these can be hooked into by monitoring tools (NewRelic, Rack::Bug, etc) to track memcached usage.
3. supports SASL for use in managed environments, e.g. Heroku.
4. provides proper failover with recovery and adjustable timeouts
Supported Ruby versions and implementations
------------------------------------------------
Dalli should work identically on:
* JRuby 1.6+
* Ruby 1.9.3+
* Rubinius 2.0
If you have problems, please enter an issue.
Installation and Usage
------------------------
Remember, Dalli **requires** memcached 1.4+. You can check the version with `memcached -h`. Please note that memcached that Mac OS X Snow Leopard ships with is 1.2.8 and won't work. Install 1.4.x using Homebrew with
brew install memcached
On Ubuntu you can install it by running:
apt-get install memcached
You can verify your installation using this piece of code:
```bash
gem install dalli
```
```ruby
require 'dalli'
options = { :namespace => "app_v1", :compress => true }
dc = Dalli::Client.new('localhost:11211', options)
dc.set('abc', 123)
value = dc.get('abc')
```
The test suite requires memcached 1.4.3+ with SASL enabled (brew install memcached --enable-sasl ; mv /usr/bin/memcached /usr/bin/memcached.old). Currently only supports the PLAIN mechanism.
Dalli has no runtime dependencies and never will. You can optionally install the 'kgio' gem to
give Dalli a 20-30% performance boost.
Usage with Rails 3.x and 4.x
---------------------------
In your Gemfile:
```ruby
gem 'dalli'
```
In `config/environments/production.rb`:
```ruby
config.cache_store = :dalli_store
```
Here's a more comprehensive example that sets a reasonable default for maximum cache entry lifetime (one day), enables compression for large values and namespaces all entries for this rails app. Remove the namespace if you have multiple apps which share cached values.
```ruby
config.cache_store = :dalli_store, 'cache-1.example.com', 'cache-2.example.com',
{ :namespace => NAME_OF_RAILS_APP, :expires_in => 1.day, :compress => true }
```
If your servers are specified in `ENV["MEMCACHE_SERVERS"]` (e.g. on Heroku when using a third-party hosted addon), simply provide `nil` for the servers:
```ruby
config.cache_store = :dalli_store, nil, { :namespace => NAME_OF_RAILS_APP, :expires_in => 1.day, :compress => true }
```
To use Dalli for Rails session storage that times out after 20 minutes, in `config/initializers/session_store.rb`:
For Rails >= 3.2.4:
```ruby
Rails.application.config.session_store ActionDispatch::Session::CacheStore, :expire_after => 20.minutes
```
For Rails 3.x:
```ruby
require 'action_dispatch/middleware/session/dalli_store'
Rails.application.config.session_store :dalli_store, :memcache_server => ['host1', 'host2'], :namespace => 'sessions', :key => '_foundation_session', :expire_after => 20.minutes
```
Dalli does not support Rails 2.x.
Multithreading and Rails
--------------------------
If you use Puma or another threaded app server, as of Dalli 2.7, you can use a pool
of Dalli clients with Rails to ensure the `Rails.cache` singleton does not become a
source of thread contention. You must add `gem 'connection_pool'` to your Gemfile and
add :pool\_size to your `dalli_store` config:
```ruby
config.cache_store = :dalli_store, 'cache-1.example.com', { :pool_size => 5 }
```
You can then use the Rails cache as normal or check out a Dalli client directly from the pool:
```ruby
Rails.cache.fetch('foo', :expires_in => 300) do
'bar'
end
Rails.cache.dalli.with do |client|
# client is a Dalli::Client instance which you can
# use ONLY within this block
end
```
Configuration
------------------------
Dalli::Client accepts the following options. All times are in seconds.
**expires_in**: Global default for key TTL. Default is 0, which means no expiry.
**failover**: Boolean, if true Dalli will failover to another server if the main server for a key is down.
**compress**: Boolean, if true Dalli will gzip-compress values larger than 1K.
**compression_min_size**: Minimum value byte size for which to attempt compression. Default is 1K.
**compression_max_size**: Maximum value byte size for which to attempt compression. Default is unlimited.
**serializer**: The serializer to use for objects being stored (ex. JSON).
Default is Marshal.
**socket_timeout**: Timeout for all socket operations (connect, read, write). Default is 0.5.
**socket_max_failures**: When a socket operation fails after socket_timeout, the same operation is retried. This is to not immediately mark a server down when there's a very slight network problem. Default is 2.
**socket_failure_delay**: Before retrying a socket operation, the process sleeps for this amount of time. Default is 0.01. Set to nil for no delay.
**down_retry_delay**: When a server has been marked down due to many failures, the server will be checked again for being alive only after this amount of time. Don't set this value to low, otherwise each request which tries the failed server might hang for the maximum **socket_timeout**. Default is 1 second.
**value_max_bytes**: The maximum size of a value in memcached. Defaults to 1MB, this can be increased with memcached's -I parameter. You must also configure Dalli to allow the larger size here.
**username**: The username to use for authenticating this client instance against a SASL-enabled memcached server. Heroku users should not need to use this normally.
**password**: The password to use for authenticating this client instance against a SASL-enabled memcached server. Heroku users should not need to use this normally.
**keepalive**: Boolean. If true, Dalli will enable keep-alive for socket connections. Default is true.
**compressor**: The compressor to use for objects being stored.
Default is zlib, implemented under `Dalli::Compressor`.
If serving compressed data using nginx's HttpMemcachedModule, set `memcached_gzip_flag 2` and use `Dalli::GzipCompressor`
Features and Changes
------------------------
By default, Dalli is thread-safe. Disable thread-safety at your own peril.
Dalli does not need anything special in Unicorn/Passenger since 2.0.4.
It will detect sockets shared with child processes and gracefully reopen the
socket.
Note that Dalli does not require ActiveSupport or Rails. You can safely use it in your own Ruby projects.
[View the Client API](http://www.rubydoc.info/github/mperham/dalli/Dalli/Client)
Helping Out
-------------
If you have a fix you wish to provide, please fork the code, fix in your local project and then send a pull request on github. Please ensure that you include a test which verifies your fix and update History.md with a one sentence description of your fix so you get credit as a contributor.
We're not accepting new compressors. They are trivial to add in an initializer. See #385 (LZ4), #406 (Snappy)
Thanks
------------
Eric Wong - for help using his [kgio](http://unicorn.bogomips.org/kgio/index.html) library.
Brian Mitchell - for his remix-stash project which was helpful when implementing and testing the binary protocol support.
[CouchBase](http://couchbase.com) - for their project sponsorship
Author
----------
Mike Perham, [mikeperham.com](http://mikeperham.com), [@mperham](http://twitter.com/mperham)
Copyright
-----------
Copyright (c) Mike Perham. See LICENSE for details.

@ -0,0 +1,42 @@
require 'appraisal'
require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'test'
test.pattern = 'test/**/test_*.rb'
test.warning = true
test.verbose = true
end
Rake::TestTask.new(:bench) do |test|
test.libs << 'test'
test.pattern = 'test/benchmark_test.rb'
end
begin
require 'metric_fu'
MetricFu::Configuration.run do |config|
config.rcov[:rcov_opts] << "-Itest:lib"
end
rescue LoadError
end
task :default => :test
task :test_all do
system('rake test RAILS_VERSION="~> 3.0.0"')
system('rake test RAILS_VERSION=">= 3.0.0"')
end
# 'gem install rdoc' to upgrade RDoc if this is giving you errors
begin
require 'rdoc/task'
RDoc::Task.new do |rd|
rd.rdoc_files.include("lib/**/*.rb")
end
rescue LoadError
puts "Unable to load rdoc, run 'gem install rdoc' to fix this."
end
require 'rake/clean'
CLEAN.include "**/*.rbc"
CLEAN.include "**/.DS_Store"

@ -0,0 +1,29 @@
require './lib/dalli/version'
Gem::Specification.new do |s|
s.name = %q{dalli}
s.version = Dalli::VERSION
s.license = "MIT"
s.authors = ["Mike Perham"]
s.description = %q{High performance memcached client for Ruby}
s.email = %q{mperham@gmail.com}
s.files = Dir.glob("lib/**/*") + [
"LICENSE",
"README.md",
"History.md",
"Rakefile",
"Gemfile",
"dalli.gemspec",
"Performance.md",
]
s.homepage = %q{http://github.com/mperham/dalli}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.summary = %q{High performance memcached client for Ruby}
s.test_files = Dir.glob("test/**/*")
s.add_development_dependency(%q<minitest>, [">= 4.2.0"])
s.add_development_dependency(%q<mocha>, [">= 0"])
s.add_development_dependency(%q<rails>, ["~> 4"])
end

@ -0,0 +1,81 @@
require 'active_support/cache'
require 'action_dispatch/middleware/session/abstract_store'
require 'dalli'
# Dalli-based session store for Rails 3.0.
module ActionDispatch
module Session
class DalliStore < AbstractStore
def initialize(app, options = {})
# Support old :expires option
options[:expire_after] ||= options[:expires]
super
@default_options = { :namespace => 'rack:session' }.merge(@default_options)
@pool = options[:cache] || begin
Dalli::Client.new(
@default_options[:memcache_server], @default_options)
end
@namespace = @default_options[:namespace]
@raise_errors = !!@default_options[:raise_errors]
super
end
def reset
@pool.reset
end
private
def get_session(env, sid)
sid = generate_sid unless sid and !sid.empty?
begin
session = @pool.get(sid) || {}
rescue Dalli::DalliError => ex
# re-raise ArgumentError so Rails' session abstract_store.rb can autoload any missing models
raise ArgumentError, ex.message if ex.message =~ /unmarshal/
Rails.logger.warn("Session::DalliStore#get: #{ex.message}")
session = {}
end
[sid, session]
end
def set_session(env, sid, session_data, options = nil)
options ||= env[ENV_SESSION_OPTIONS_KEY]
expiry = options[:expire_after]
@pool.set(sid, session_data, expiry)
sid
rescue Dalli::DalliError
Rails.logger.warn("Session::DalliStore#set: #{$!.message}")
raise if @raise_errors
false
end
def destroy_session(env, session_id, options)
begin
@pool.delete(session_id)
rescue Dalli::DalliError
Rails.logger.warn("Session::DalliStore#destroy_session: #{$!.message}")
raise if @raise_errors
end
return nil if options[:drop]
generate_sid
end
def destroy(env)
if sid = current_session_id(env)
@pool.delete(sid)
end
rescue Dalli::DalliError
Rails.logger.warn("Session::DalliStore#destroy: #{$!.message}")
raise if @raise_errors
false
end
end
end
end

File diff suppressed because it is too large Load Diff

@ -0,0 +1,46 @@
require 'dalli/compressor'
require 'dalli/client'
require 'dalli/ring'
require 'dalli/server'
require 'dalli/socket'
require 'dalli/version'
require 'dalli/options'
require 'dalli/railtie' if defined?(::Rails::Railtie)
module Dalli
# generic error
class DalliError < RuntimeError; end
# socket/server communication error
class NetworkError < DalliError; end
# no server available/alive error
class RingError < DalliError; end
# application error in marshalling serialization
class MarshalError < DalliError; end
# application error in marshalling deserialization or decompression
class UnmarshalError < DalliError; end
def self.logger
@logger ||= (rails_logger || default_logger)
end
def self.rails_logger
(defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger) ||
(defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER.respond_to?(:debug) && RAILS_DEFAULT_LOGGER)
end
def self.default_logger
require 'logger'
l = Logger.new(STDOUT)
l.level = Logger::INFO
l
end
def self.logger=(logger)
@logger = logger
end
end
if defined?(RAILS_VERSION) && RAILS_VERSION < '3'
raise Dalli::DalliError, "Dalli #{Dalli::VERSION} does not support Rails version < 3.0"
end

@ -0,0 +1,58 @@
require 'dalli/client'
module Dalli
class Client
##
# Get the value and CAS ID associated with the key. If a block is provided,
# value and CAS will be passed to the block.
def get_cas(key)
(value, cas) = perform(:cas, key)
value = (!value || value == 'Not found') ? nil : value
if block_given?
yield value, cas
else
[value, cas]
end
end
##
# Fetch multiple keys efficiently, including available metadata such as CAS.
# If a block is given, yields key/data pairs one a time. Data is an array:
# [value, cas_id]
# If no block is given, returns a hash of
# { 'key' => [value, cas_id] }
def get_multi_cas(*keys)
if block_given?
get_multi_yielder(keys) {|*args| yield(*args)}
else
Hash.new.tap do |hash|
get_multi_yielder(keys) {|k, data| hash[k] = data}
end
end
end
##
# Set the key-value pair, verifying existing CAS.
# Returns the resulting CAS value if succeeded, and falsy otherwise.
def set_cas(key, value, cas, ttl=nil, options=nil)
ttl ||= @options[:expires_in].to_i
perform(:set, key, value, ttl, cas, options)
end
##
# Conditionally add a key/value pair, verifying existing CAS, only if the
# key already exists on the server. Returns the new CAS value if the
# operation succeeded, or falsy otherwise.
def replace_cas(key, value, cas, ttl=nil, options=nil)
ttl ||= @options[:expires_in].to_i
perform(:replace, key, value, ttl, cas, options)
end
# Delete a key/value pair, verifying existing CAS.
# Returns true if succeeded, and falsy otherwise.
def delete_cas(key, cas=0)
perform(:delete, key, cas)
end
end
end

File diff suppressed because it is too large Load Diff

@ -0,0 +1,29 @@
require 'zlib'
require 'stringio'
module Dalli
class Compressor
def self.compress(data)
Zlib::Deflate.deflate(data)
end
def self.decompress(data)
Zlib::Inflate.inflate(data)
end
end
class GzipCompressor
def self.compress(data)
io = StringIO.new("w")
gz = Zlib::GzipWriter.new(io)
gz.write(data)
gz.close
io.string
end
def self.decompress(data)
io = StringIO.new(data, "rb")
Zlib::GzipReader.new(io).read
end
end
end

@ -0,0 +1,64 @@
require 'thread'
require 'monitor'
module Dalli
# Make Dalli threadsafe by using a lock around all
# public server methods.
#
# Dalli::Server.extend(Dalli::Threadsafe)
#
module Threadsafe
def self.extended(obj)
obj.init_threadsafe
end
def request(op, *args)
@lock.synchronize do
super
end
end
def alive?
@lock.synchronize do
super
end
end
def close
@lock.synchronize do
super
end
end
def multi_response_start
@lock.synchronize do
super
end
end
def multi_response_nonblock
@lock.synchronize do
super
end
end
def multi_response_abort
@lock.synchronize do
super
end
end
def lock!
@lock.mon_enter
end
def unlock!
@lock.mon_exit
end
def init_threadsafe
@lock = Monitor.new
end
end
end

@ -0,0 +1,7 @@
module Dalli
class Railtie < ::Rails::Railtie
config.before_configuration do
config.cache_store = :dalli_store
end
end
end

@ -0,0 +1,142 @@
require 'digest/sha1'
require 'zlib'
module Dalli
class Ring
POINTS_PER_SERVER = 160 # this is the default in libmemcached
attr_accessor :servers, :continuum
def initialize(servers, options)
@servers = servers
@continuum = nil
if servers.size > 1
total_weight = servers.inject(0) { |memo, srv| memo + srv.weight }
continuum = []
servers.each do |server|
entry_count_for(server, servers.size, total_weight).times do |idx|
hash = Digest::SHA1.hexdigest("#{server.hostname}:#{server.port}:#{idx}")
value = Integer("0x#{hash[0..7]}")
continuum << Dalli::Ring::Entry.new(value, server)
end
end
@continuum = continuum.sort { |a, b| a.value <=> b.value }
end
threadsafe! unless options[:threadsafe] == false
@failover = options[:failover] != false
end
def server_for_key(key)
if @continuum
hkey = hash_for(key)
20.times do |try|
entryidx = binary_search(@continuum, hkey)
server = @continuum[entryidx].server
return server if server.alive?
break unless @failover
hkey = hash_for("#{try}#{key}")
end
else
server = @servers.first
return server if server && server.alive?
end
raise Dalli::RingError, "No server available"
end
def lock
@servers.each { |s| s.lock! }
begin
return yield
ensure
@servers.each { |s| s.unlock! }
end
end
private
def threadsafe!
@servers.each do |s|
s.extend(Dalli::Threadsafe)
end
end
def hash_for(key)
Zlib.crc32(key)
end
def entry_count_for(server, total_servers, total_weight)
((total_servers * POINTS_PER_SERVER * server.weight) / Float(total_weight)).floor
end
# Native extension to perform the binary search within the continuum
# space. Fallback to a pure Ruby version if the compilation doesn't work.
# optional for performance and only necessary if you are using multiple
# memcached servers.
begin
require 'inline'
inline do |builder|
builder.c <<-EOM
int binary_search(VALUE ary, unsigned int r) {
long upper = RARRAY_LEN(ary) - 1;
long lower = 0;
long idx = 0;
ID value = rb_intern("value");
VALUE continuumValue;
unsigned int l;
while (lower <= upper) {
idx = (lower + upper) / 2;
continuumValue = rb_funcall(RARRAY_PTR(ary)[idx], value, 0);
l = NUM2UINT(continuumValue);
if (l == r) {
return idx;
}
else if (l > r) {
upper = idx - 1;
}
else {
lower = idx + 1;
}
}
return upper;
}
EOM
end
rescue LoadError
# Find the closest index in the Ring with value <= the given value
def binary_search(ary, value)
upper = ary.size - 1
lower = 0
idx = 0
while (lower <= upper) do
idx = (lower + upper) / 2
comp = ary[idx].value <=> value
if comp == 0
return idx
elsif comp > 0
upper = idx - 1
else
lower = idx + 1
end
end
return upper
end
end
class Entry
attr_reader :value
attr_reader :server
def initialize(val, srv)
@value = val
@server = srv
end
end
end
end

File diff suppressed because it is too large Load Diff

@ -0,0 +1,108 @@
begin
require 'kgio'
puts "Using kgio socket IO" if defined?($TESTING) && $TESTING
class Dalli::Server::KSocket < Kgio::Socket
attr_accessor :options, :server
def kgio_wait_readable
IO.select([self], nil, nil, options[:socket_timeout]) || raise(Timeout::Error, "IO timeout")
end
def kgio_wait_writable
IO.select(nil, [self], nil, options[:socket_timeout]) || raise(Timeout::Error, "IO timeout")
end
def self.open(host, port, server, options = {})
addr = Socket.pack_sockaddr_in(port, host)
sock = start(addr)
sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true)
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true) if options[:keepalive]
sock.options = options
sock.server = server
sock.kgio_wait_writable
sock
end
alias :write :kgio_write
def readfull(count)
value = ''
loop do
value << kgio_read!(count - value.bytesize)
break if value.bytesize == count
end
value
end
def read_available
value = ''
loop do
ret = kgio_tryread(8196)
case ret
when nil
raise EOFError, 'end of stream'
when :wait_readable
break
else
value << ret
end
end
value
end
end
if ::Kgio.respond_to?(:wait_readable=)
::Kgio.wait_readable = :kgio_wait_readable
::Kgio.wait_writable = :kgio_wait_writable
end
rescue LoadError
puts "Using standard socket IO (#{RUBY_DESCRIPTION})" if defined?($TESTING) && $TESTING
class Dalli::Server::KSocket < TCPSocket
attr_accessor :options, :server
def self.open(host, port, server, options = {})
Timeout.timeout(options[:socket_timeout]) do
sock = new(host, port)
sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true)
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true) if options[:keepalive]
sock.options = { :host => host, :port => port }.merge(options)
sock.server = server
sock
end
end
def readfull(count)
value = ''
begin
loop do
value << read_nonblock(count - value.bytesize)
break if value.bytesize == count
end
rescue Errno::EAGAIN, Errno::EWOULDBLOCK
if IO.select([self], nil, nil, options[:socket_timeout])
retry
else
raise Timeout::Error, "IO timeout: #{options.inspect}"
end
end
value
end
def read_available
value = ''
loop do
begin
value << read_nonblock(8196)
rescue Errno::EAGAIN, Errno::EWOULDBLOCK
break
end
end
value
end
end
end

@ -0,0 +1,3 @@
module Dalli
VERSION = '2.7.2'
end

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save