Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Cassandra backend.

  • Loading branch information...
commit 05ae9e0f2b3b8d202550c78cb0d9795e103c56c6 1 parent 46ef575
@hanula hanula authored
Showing with 76 additions and 0 deletions.
  1. +75 −0 beaker_extensions/cassandra.py
  2. +1 −0  setup.py
View
75 beaker_extensions/cassandra.py
@@ -0,0 +1,75 @@
+import logging
+from beaker.exceptions import InvalidCacheBackendError, MissingCacheParameter
+
+from beaker_extensions.nosql import Container
+from beaker_extensions.nosql import NoSqlManager
+from beaker_extensions.nosql import pickle
+
+try:
+ import pycassa
+except ImportError:
+ raise InvalidCacheBackendError("Cassandra cache backend requires the 'pycassa' library")
+
+log = logging.getLogger(__name__)
+
+class CassandraManager(NoSqlManager):
+ """
+ Cassandra backend for beaker.
+
+ Configuration example:
+ beaker.session.type = cassandra
+ beaker.session.url = localhost:9160
+ beaker.session.keyspace = Keyspace1
+ beaker.session.column_family = beaker
+
+ The default column_family is 'beaker'.
+ If it doesn't exist under given keyspace, it is created automatically.
+ """
+ def __init__(self, namespace, url=None, data_dir=None, lock_dir=None, keyspace=None, column_family=None, **params):
+ if not keyspace:
+ raise MissingCacheParameter("keyspace is required")
+ self.keyspace = keyspace
+ self.column_family = column_family or 'beaker'
+ NoSqlManager.__init__(self, namespace, url=url, data_dir=data_dir, lock_dir=lock_dir, **params)
+
+ def open_connection(self, host, port, **params):
+ self.pool = pycassa.ConnectionPool(self.keyspace)
+ try:
+ self.cf = pycassa.ColumnFamily(self.pool, self.column_family)
+ except pycassa.NotFoundException:
+ log.info("Creating new %s ColumnFamily." % self.column_family)
+ system_manager = pycassa.system_manager.SystemManager()
+ system_manager.create_column_family(self.keyspace, self.column_family)
+ self.cf = pycassa.ColumnFamily(self.pool, self.column_family)
+
+ def __contains__(self, key):
+ return self.cf.get_count(self._format_key(key)) > 0
+
+ def set_value(self, key, value, expiretime=None):
+ key = self._format_key(key)
+ self.cf.insert(key, {'data': pickle.dumps(value)}, ttl=expiretime)
+
+ def __getitem__(self, key):
+ try:
+ result = self.cf.get(key)
+ return result['data']
+ except pycassa.NotFoundException:
+ return None
+
+ def __delitem__(self, key):
+ key = self._format_key(key)
+ self.cf.remove(self._format_key(key))
+
+ def _format_key(self, key):
+ return '%s:%s' % (self.namespace, key.replace(' ', '\302\267'))
+
+ def do_remove(self):
+ for key, empty in cf.get_range(column_count=0, filter_empty=False):
+ cf.remove(key)
+
+ def keys(self):
+ return list(key for key, empty in self.cf.get_range(column_count=0, filter_empty=False))
+
+
+class CassandraContainer(Container):
+ namespace_manager = CassandraManager
View
1  setup.py
@@ -28,5 +28,6 @@
riak = beaker_extensions.riak:RiakManager
dynomite = beaker_extensions.dynomite_:DynomiteManager
ringo = beaker_extensions.ringo:RingoManager
+ cassandra = beaker_extensions.cassandra:CassandraManager
""",
)
Please sign in to comment.
Something went wrong with that request. Please try again.