Class: Moneta::Shared

### Home Classes Methods

### In Files

- lib/moneta/shared.rb

### Parent

Wrapper

### Methods

- ::new
- #close
- #connect
- #start_server
- #wrap

### Files

- LICENSE

### Class/Module Index

Quicksearch

- Moneta
- Moneta::Adapters
- Moneta::Adapters::ActiveRecord
- Moneta::Adapters::Cassandra
- Moneta::Adapters::Client
- Moneta::Adapters::Cookie
- Moneta::Adapters::Couch
- Moneta::Adapters::DBM
- Moneta::Adapters::DataMapper
- Moneta::Adapters::DataMapper::Store
- Moneta::Adapters::Daybreak
- Moneta::Adapters::File
- Moneta::Adapters::Fog
- Moneta::Adapters::GDBM
- Moneta::Adapters::HBase
- Moneta::Adapters::KyotoCabinet
- Moneta::Adapters::LMDB
- Moneta::Adapters::LRUHash
- Moneta::Adapters::LRUHash::Entry
- Moneta::Adapters::LevelDB
- Moneta::Adapters::LocalMemCache
- Moneta::Adapters::MemcachedDalli
- Moneta::Adapters::MemcachedNative
- Moneta::Adapters::Memory
- Moneta::Adapters::MongoBase
- Moneta::Adapters::MongoMoped
- Moneta::Adapters::MongoOfficial
- Moneta::Adapters::Null
- Moneta::Adapters::PStore
- Moneta::Adapters::Redis
- Moneta::Adapters::RestClient
- Moneta::Adapters::Riak
- Moneta::Adapters::SDBM
- Moneta::Adapters::Sequel
- Moneta::Adapters::Sqlite
- Moneta::Adapters::TDB
- Moneta::Adapters::TokyoCabinet
- Moneta::Adapters::TokyoTyrant
- Moneta::Adapters::YAML
- Moneta::Builder
- Moneta::Cache
- Moneta::Cache::DSL
- Moneta::CreateSupport
- Moneta::Defaults
- Moneta::Defaults::ClassMethods
- Moneta::Expires
- Moneta::ExpiresSupport
- Moneta::HashAdapter
- Moneta::IncrementSupport
- Moneta::Lock
- Moneta::Logger
- Moneta::Logger::Format
- Moneta::Mutex
- Moneta::OptionMerger
- Moneta::OptionSupport
- Moneta::Pool
- Moneta::Proxy
- Moneta::Semaphore
- Moneta::Server
- Moneta::Shared
- Moneta::Stack
- Moneta::Stack::DSL
- Moneta::SynchronizePrimitive
- Moneta::Transformer
- Moneta::Transformer::Helper
- Moneta::Utils
- Moneta::WeakCreate
- Moneta::WeakIncrement
- Moneta::Wrapper
- Rack
- Rack::Cache
- Rack::Cache::EntityStore
- Rack::Cache::EntityStore::MONETA
- Rack::Cache::EntityStore::Moneta
- Rack::Cache::MetaStore
- Rack::Cache::MetaStore::MONETA
- Rack::Cache::MetaStore::Moneta
- Rack::Cache::MonetaResolver
- Rack::MonetaCookies
- Rack::MonetaRest
- Rack::MonetaStore
- Rack::Session
- Rack::Session::Moneta
- ActionDispatch
- ActionDispatch::Session
- ActionDispatch::Session::MonetaStore
- ActiveSupport
- ActiveSupport::Cache
- ActiveSupport::Cache::MonetaStore

No matching classes.

# Moneta::Shared

Shares a store between processes

@example Share a store

    Moneta.build do
      use :Transformer, key: :marshal, value: :marshal
      use :Shared do
        adapter :GDBM, file: 'shared.db'
      end
    end

@api public

### Public Class Methods

new(options = {}, &block) click to toggle source

@param [Hash] options @option options [Integer] :port (9000) TCP port @option options [String] :host Server hostname @option options [String] :socket Unix socket file name

    # File lib/moneta/shared.rb, line 18
    def initialize(options = {}, &block)
      @options = options
      @builder = Builder.new(&block)
    end

### Public Instance Methods

close() click to toggle source

(see Proxy#close)

    # File lib/moneta/shared.rb, line 24
    def close
      if @server
        @server.stop
        @thread.join
        @server = @thread = nil
      end
      if @adapter
        @adapter.close
        @adapter = nil
      end
    end

### Protected Instance Methods

connect() click to toggle source

    # File lib/moneta/shared.rb, line 43
    def connect
      @adapter ||= Adapters::Client.new(@options)
    rescue Errno::ECONNREFUSED, Errno::ENOENT => ex
      start_server
      tries ||= 0
      warn "Moneta::Shared - Failed to connect: #{ex.message}" if tries > 0
      (tries += 1) < 3 ? retry : raise
    end

start_server() click to toggle source

TODO: Implement this using forking (MRI) and threading (JRuby) to get maximal performance

    # File lib/moneta/shared.rb, line 54
    def start_server
      @adapter = Lock.new(@builder.build.last)
      @server = Server.new(@adapter, @options)
      @thread = Thread.new { @server.run }
      sleep 0.1 until @server.running?
    rescue Exception => ex
      @adapter.close if @adapter
      @adapter = nil
      @server = nil
      @thread = nil
      warn "Moneta::Shared - Failed to start server: #{ex.message}"
    end

wrap(*args) click to toggle source

    # File lib/moneta/shared.rb, line 38
    def wrap(*args)
      connect
      yield
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.