Browse Source

initial commit

master
Brett Langdon 12 years ago
commit
0d6d22e0e5
10 changed files with 283 additions and 0 deletions
  1. +5
    -0
      .gitignore
  2. +21
    -0
      LICENSE.txt
  3. +4
    -0
      MANIFEST.ini
  4. +4
    -0
      README.md
  5. +1
    -0
      requirements.txt
  6. +1
    -0
      riakcached/__init__.py
  7. +197
    -0
      riakcached/client.py
  8. +23
    -0
      riakcached/exceptions.py
  9. +0
    -0
      riakcached/test/__init__.py
  10. +27
    -0
      setup.py

+ 5
- 0
.gitignore View File

@ -0,0 +1,5 @@
.ropeproject
*.py[co]
*.log
build
dist

+ 21
- 0
LICENSE.txt View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2013 Brett Langdon <brett@blangdon.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

+ 4
- 0
MANIFEST.ini View File

@ -0,0 +1,4 @@
include README.* setup.py setup.cfg
recursive-include riakcached *.py
global-exclude *.pyc
global-exclude *.pyo

+ 4
- 0
README.md View File

@ -0,0 +1,4 @@
Riakcached
==========
A Memcached like interface to the Riak HTTP Client.

+ 1
- 0
requirements.txt View File

@ -0,0 +1 @@
urllib3==1.7

+ 1
- 0
riakcached/__init__.py View File

@ -0,0 +1 @@
__version__ = "0.1.0"

+ 197
- 0
riakcached/client.py View File

@ -0,0 +1,197 @@
__all__ = ["RiakClient"]
import json
import Queue
import threading
import urllib3
from riakcached import exceptions
class RiakClient(object):
"""
"""
__slots__ = [
"_serializers",
"_deserializers",
"_pool",
"_timeout",
"bucket",
"url",
]
_serializers = {
"application/json": json.dumps,
}
_deserializers = {
"application/json": json.loads,
}
def __init__(self, bucket, url="http://127.0.0.1:8098", timeout=2):
"""
"""
self.bucket = bucket
self.url = url.strip("/")
self._timeout = timeout
self._connect()
def set_serializer(self, content_type, serializer, deserializer):
"""
"""
content_type = content_type.lower()
self._serializers[content_type] = serializer
self._deserializers[content_type] = deserializer
def close(self):
"""
"""
if self._pool:
self._pool.close()
def get(self, key):
"""
"""
response = self._request(
method="GET",
url="%s/riak/%s/%s" % (self.url, self.bucket, key),
)
if response.status == 400:
raise exceptions.RiakcachedBadRequest(response.data)
elif response.status == 503:
raise exceptions.RiakcachedServiceUnavailable(response.data)
if response.status not in (200, 300, 304):
return None
deserializer = self._deserializers.get(response.getheader("content-type"), str)
return deserializer(response.data)
def get_many(self, keys):
"""
"""
def worker(key, results):
results.put((key, self.get(key)))
args = [[key] for key in keys]
results = self._many(worker, args)
results = dict((key, value) for key, value in results.iteritems() if value is not None)
return results or None
def set(self, key, value, content_type="text/plain"):
"""
"""
serializer = self._serializers.get(content_type.lower(), str)
value = serializer(value)
response = self._request(
method="POST",
url="%s/riak/%s/%s" % (self.url, self.bucket, key),
body=value,
headers={
"Content-Type": content_type,
},
)
if response.status == 400:
raise exceptions.RiakcachedBadRequest(response.data)
elif response.status == 412:
raise exceptions.RiakcachedPreconditionFailed(response.data)
return response.status in (200, 201, 204, 300)
def set_many(self, values):
"""
"""
def worker(key, value, results):
results.put((key, self.set(key, value)))
args = [list(data) for data in values.items()]
return self._many(worker, args)
def delete(self, key):
"""
"""
response = self._request(
method="DELETE",
url="%s/riak/%s/%s" % (self.url, self.bucket, key),
)
if response.status == 400:
raise exceptions.RiakcachedBadRequest(response.data)
return response.status in (204, 404)
def delete_many(self, keys):
"""
"""
def worker(key, results):
results.put((key, self.delete(key)))
args = [[key] for key in keys]
return self._many(worker, args)
def stats(self):
"""
"""
response = self._request(
method="GET",
url="%s/stats" % self.url,
)
if response.status == 200:
return json.loads(response.data)
return None
def ping(self):
"""
"""
response = self._request(
method="GET",
url="%s/ping" % self.url,
)
return response.status == 200
def incr(self, key, value=1):
"""
"""
response = self._request(
method="POST",
url="%s/riak/%s/counters/%s" % (self.url, self.bucket, key),
body=str(value),
)
if response.status == 409:
raise exceptions.RiakcachedConflict(response.data)
return True
def _connect(self):
self._pool = urllib3.connection_from_url(self.url)
def _request(self, method, url, body=None, headers=None):
try:
return self._pool.urlopen(
method=method,
url=url,
body=body,
headers=headers,
timeout=self._timeout,
redirect=False,
)
except urllib3.exceptions.TimeoutError, e:
raise exceptions.RiakcachedTimeout(e.message)
except urllib3.exceptions.HTTPError, e:
raise exceptions.RiakcachedConnectionError(e.message)
def _many(self, target, args_list):
workers = []
worker_results = Queue.Queue()
for args in args_list:
args.append(worker_results)
worker = threading.Thread(target=target, args=args)
worker.daemon = True
worker.start()
workers.append(worker)
for worker in workers:
worker.join()
results = {}
while not worker_results.empty():
key, value = worker_results.get()
results[key] = value
return results

+ 23
- 0
riakcached/exceptions.py View File

@ -0,0 +1,23 @@
class RiakcachedException(Exception):
pass
class RiakcachedBadRequest(RiakcachedException):
pass
class RiakcachedNotFound(RiakcachedException):
pass
class RiakcachedServiceUnavailable(RiakcachedException):
pass
class RiakcachedPreconditionFailed(RiakcachedException):
pass
class RiakcachedConflict(RiakcachedException):
pass
class RiakcachedTimeout(RiakcachedException):
pass
class RiakcachedConnectionError(RiakcachedException):
pass

+ 0
- 0
riakcached/test/__init__.py View File


+ 27
- 0
setup.py View File

@ -0,0 +1,27 @@
#!/usr/bin/env python
from setuptools import setup, find_packages
from riakcached import __version__
setup(
name="riakcached",
version=__version__,
author="Brett Langdon",
author_email="brett@blangdon.com",
packages=find_packages(),
install_requires=["urllib3==1.7"],
setup_requires=["nose>=1.0"],
description="A Memcached like interface to Riak",
long_description=open("README.md").read(),
license="MIT",
url='https://github.com/brettlangdon/riakcached',
classifiers=[
"Intended Audience :: Developers",
"Programming Language :: Python",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7",
"License :: OSI Approved :: MIT License",
"Topic :: Database",
],
)

Loading…
Cancel
Save