Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for CouchDB.

  • Loading branch information...
commit 2987ae3b627a3317b15701e902d86c5d670795b9 1 parent df34556
Quin Hoxie authored committed
Showing with 87 additions and 0 deletions.
  1. +69 −0 lib/moneta/couch.rb
  2. +18 −0 spec/moneta_couch_spec.rb
View
69 lib/moneta/couch.rb
@@ -0,0 +1,69 @@
+begin
+ require "couchrest"
+rescue LoadError
+ puts "You need the couchrest gem to use the CouchDB store"
+ exit
+end
+
+module Moneta
+ class Couch
+ def initialize(options = {})
+ @db = ::CouchRest.database!(options[:db])
+ unless options[:skip_expires]
+ @expiration = Moneta::Couch.new(:db => "#{options[:db]}_expiration", :skip_expires => true)
+ self.extend(StringExpires)
+ end
+ end
+
+ def key?(key)
+ !self[key].nil?
+ rescue RestClient::ResourceNotFound
+ false
+ end
+
+ alias has_key? key?
+
+ def [](key)
+ @db.get(key)["data"]
+ rescue RestClient::ResourceNotFound
+ nil
+ end
+
+ def []=(key, value)
+ @db.save_doc("_id" => key, :data => value)
+ rescue RestClient::RequestFailed
+ self[key]
+ end
+
+ def fetch(key, default)
+ self[key] || default
+ end
+
+ def delete(key)
+ value = @db.get(key)
+ @db.delete_doc({"_id" => value["_id"], "_rev" => value["_rev"]}) if value
+ value["data"]
+ rescue RestClient::ResourceNotFound
+ nil
+ end
+
+ def store(key, value, options = {})
+ self[key] = value
+ end
+
+ def update_key(key, options = {})
+ val = self[key]
+ self.store(key, val, options)
+ rescue RestClient::ResourceNotFound
+ nil
+ end
+
+ def clear
+ @db.recreate!
+ end
+
+ def delete_store
+ @db.delete!
+ end
+ end
+end
View
18 spec/moneta_couch_spec.rb
@@ -0,0 +1,18 @@
+require File.dirname(__FILE__) + '/spec_helper'
+require 'moneta/couch'
+
+describe "Moneta::Couch" do
+ before(:each) do
+ @cache = Moneta::Couch.new(:db => "couch_test")
+ @expiration = Moneta::Couch.new(:db => "couch_test_expiration", :skip_expires => true)
+ @cache.clear
+ @expiration.clear
+ end
+
+ after(:all) do
+ @cache.delete_store
+ @expiration.delete_store
+ end
+
+ it_should_behave_like "a read/write Moneta cache"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.