Sha256: e3140eb22326ab8529dad043479f77f7803021093b34bd247fc6f2b56d4eeb85

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

require 'og'
require 'base64'

module Glue

  class OgCached
    prop_accessor :unique_id, String, :unique => true
    prop_accessor :expires, Time
    prop_accessor :cache_name, String
    prop_accessor :content
  end

  class OgCache

    def initialize(cache_name, keepalive = nil)
      @cache_name = cache_name
      @keepalive = keepalive
    end

    def []=(k,v)
      unless s = OgCached.find_by_unique_id_and_cache_name(k.to_s, @cache_name)
        s = OgCached.new
        s.cache_name = @cache_name
        s.expires = Time.now + @keepalive if @keepalive
        s.unique_id = k.to_s
      end
      #s.content = v.to_yaml
      s.content = encode(v)
      s.save
    end

    def [](k)
      s = OgCached.find_by_unique_id_and_cache_name(k.to_s, @cache_name) and decode(s.content)
    end

    def gc!
      OgCached.find(:condition => ["expires < ? AND cache_name = ?", Time.now, @cache_name]).each {|s| s.delete }
    end

    def all
      OgCached.find_by_cache_name(@cache_name)
    end

    private

    def encode(c)
      Base64.encode64(Marshal.dump(c))
    end

    def decode(c)
      Marshal::load(Base64.decode64(c)) 
      #s.content = YAML::load(s.content)
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
glue-0.28.0 lib/glue/cache/og.rb
glue-0.29.0 lib/glue/cache/og.rb
glue-0.30.0 lib/glue/cache/og.rb
glue-0.31.0 lib/glue/cache/og.rb