Class: Moneta::Adapters::File

### Home Classes Methods

### In Files

- lib/moneta/adapters/file.rb

### Parent

Object

### Methods

- ::new
- #clear
- #create
- #delete
- #increment
- #key?
- #load
- #store
- #store_path

### Included Modules

- Moneta::Defaults

### 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::Adapters::File

Filesystem backend @api public

### Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options @option options [String] :dir Directory where files will be stored

    # File lib/moneta/adapters/file.rb, line 13
    def initialize(options = {})
      raise ArgumentError, 'Option :dir is required' unless @dir = options[:dir]
      FileUtils.mkpath(@dir)
      raise "#{@dir} is not a directory" unless ::File.directory?(@dir)
    end

### Public Instance Methods

clear(options = {}) click to toggle source

(see Proxy#clear)

    # File lib/moneta/adapters/file.rb, line 52
    def clear(options = {})
      temp_dir = "#{@dir}-#{$$}-#{Thread.current.object_id}"
      ::File.rename(@dir, temp_dir)
      FileUtils.mkpath(@dir)
      self
    rescue Errno::ENOENT
      self
    ensure
      FileUtils.rm_rf(temp_dir)
    end

create(key, value, options = {}) click to toggle source

(see Proxy#create)

    # File lib/moneta/adapters/file.rb, line 83
    def create(key, value, options = {})
      path = store_path(key)
      FileUtils.mkpath(::File.dirname(path))
      # Call native java.io.File#createNewFile
      return false unless ::Java::JavaIo::File.new(path).createNewFile
      ::File.open(path, 'wb+') {|f| f.write(value) }
      true
    end

delete(key, options = {}) click to toggle source

(see Proxy#delete)

    # File lib/moneta/adapters/file.rb, line 44
    def delete(key, options = {})
      value = load(key, options)
      ::File.unlink(store_path(key))
      value
    rescue Errno::ENOENT
    end

increment(key, amount = 1, options = {}) click to toggle source

(see Proxy#increment)

    # File lib/moneta/adapters/file.rb, line 64
    def increment(key, amount = 1, options = {})
      path = store_path(key)
      FileUtils.mkpath(::File.dirname(path))
      ::File.open(path, ::File::RDWR | ::File::CREAT) do |f|
        Thread.pass until f.flock(::File::LOCK_EX)
        content = f.read
        amount += Utils.to_int(content) unless content.empty?
        content = amount.to_s
        f.pos = 0
        f.write(content)
        f.truncate(content.bytesize)
        amount
      end
    end

key?(key, options = {}) click to toggle source

(see Proxy#key?)

    # File lib/moneta/adapters/file.rb, line 20
    def key?(key, options = {})
      ::File.exist?(store_path(key))
    end

load(key, options = {}) click to toggle source

(see Proxy#load)

    # File lib/moneta/adapters/file.rb, line 25
    def load(key, options = {})
      ::File.read(store_path(key))
    rescue Errno::ENOENT
    end

store(key, value, options = {}) click to toggle source

(see Proxy#store)

    # File lib/moneta/adapters/file.rb, line 31
    def store(key, value, options = {})
      temp_file = ::File.join(@dir, "value-#{$$}-#{Thread.current.object_id}")
      path = store_path(key)
      FileUtils.mkpath(::File.dirname(path))
      ::File.open(temp_file, 'wb') {|f| f.write(value) }
      ::File.rename(temp_file, path)
      value
    rescue Exception
      File.unlink(temp_file) rescue nil
      raise
    end

### Protected Instance Methods

store_path(key) click to toggle source

    # File lib/moneta/adapters/file.rb, line 108
    def store_path(key)
      ::File.join(@dir, key)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.