Sha256: 84b5b947681a4f18e35e4349a7f0b6061b13cfc43881aeebd3d18c394ba65d39

Contents?: true

Size: 1.79 KB

Versions: 7

Compression:

Stored size: 1.79 KB

Contents

#          Copyright (c) 2009 Michael Fellinger m.fellinger@gmail.com
# All files in this distribution are subject to the terms of the Ruby license.

module Ramaze
  class Cache

    # Cache based on a Sequel model using relational databases.
    class Sequel
      include Cache::API

      class Table < ::Sequel::Model(:ramaze_cache)
        set_schema do
          primary_key :id
          string :key
          string :value
          time :expires
          index :key, :unique => true
        end

        transform :value => [
          lambda{|value| ::Marshal.load(value.unpack('m*')[0]) },
          lambda{|value| [::Marshal.dump(value)].pack('m*') }
        ]
      end

      # Setup the table, not suitable for multiple apps yet.
      def cache_setup(host, user, app, name)
        @namespace = [host, user, app, name].compact.join(':')
        Table.create_table unless Table.table_exists?
        @store = Table
      end

      # Wipe out _all_ data in the table, use with care.
      def cache_clear
        Table.delete_all
      end

      # Delete records for given +keys+
      def cache_delete(*keys)
        super do |key|
          record = @store[:key => namespaced(key)]
          record.delete if record
        end
      end

      def cache_fetch(key, default = nil)
        super{|key| @store[:key => namespaced(key)] }
      end

      def cache_store(key, value, options = {})
        key = namespaced(key)
        ttl = options[:ttl]
        expires = Time.now + ttl if ttl

        record = @store[:key => key].update(:value => value, :expires => expires)
        record.value
      rescue
        record = @store.create(:key => key, :value => value, :expires => expires)
        record.value
      end

      def namespaced(key)
        [@namespace, key].join(':')
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 3 rubygems

Version Path
Pistos-ramaze-2009.04.08 lib/ramaze/cache/sequel.rb
manveru-ramaze-2009.04.01 lib/ramaze/cache/sequel.rb
manveru-ramaze-2009.04.08 lib/ramaze/cache/sequel.rb
manveru-ramaze-2009.04.18 lib/ramaze/cache/sequel.rb
manveru-ramaze-2009.04.22 lib/ramaze/cache/sequel.rb
manveru-ramaze-2009.04 lib/ramaze/cache/sequel.rb
ramaze-2009.04 lib/ramaze/cache/sequel.rb