Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## Contributing to cache4py


### Prerequisites

Install the python package requirements using the following command:

```
pipenv install
```

### Running the tests

Run unit tests using the command:

```
pytest --cov=cache4py --cov-report html tests/
```

## Issue tracking

Create issues at [cache4py/issues](https://github.com/nitinl/cache4py/issues).

## Authors

* **Nitin Labhishetty ([lnitin94@gmail.com](mailto:lnitin94@gmail.com))**
* **Vaibhav Tulsyan ([vstulsyan@gmail.com](mailto:vstulsyan@gmail.com))**
33 changes: 10 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# cache4py
Caching simplified.
# cache4py: Caching simplified.

[![PyPI version shields.io](https://img.shields.io/pypi/v/cache4py.svg)](https://pypi.python.org/pypi/cache4py/)
[![PyPI pyversions](https://img.shields.io/pypi/pyversions/cache4py.svg)](https://pypi.python.org/pypi/cache4py/)
[![MIT license](https://img.shields.io/badge/License-MIT-blue.svg)](https://lbesson.mit-license.org/)

## Getting started

Expand All @@ -9,7 +12,7 @@ Install using pip: `$ pip install cache4py`
### Usage
```python
from cache4py.decorators import cache
from cache4py.storage.backends import RedisBackend
from cache4py.storage.redis import RedisBackend

# You can choose memcached, redis or default (python's dict) as a backend.
redis_backend = RedisBackend(url='localhost', port='6379')
Expand All @@ -22,32 +25,16 @@ def my_function_one(*args, **kwargs):

### Options
1. Keys
2. Eviction policies
2. Eviction policies (coming soon)
3. Backend
4. Max memory limit
5. Key expiry time
4. Max memory limit (coming soon)
5. Key expiry time (coming soon)



## Contributing to cache4py

These instructions will get you a copy of the project up and running on your local machine for development and testing.
TODO

### Prerequisites

Install the python package requirements using the following command:

```
pip install -r requirements.txt
```
### Running the tests

Run unit tests using the command:

```
pytest --cov=cache4py --cov-report html tests/
```
Refer CONTRIBUTING.md

## Issue tracking

Expand Down
10 changes: 5 additions & 5 deletions cache4py/storage/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ class RedisBackend(BaseBackend):
Wrapper over redis client object provided by python redis library. Supports storing objects in redis.
"""

def __init__(self, server, port=6379):
def __init__(self, url, port=6379):
"""
Initialize redis client as a cache backend.
:param server: URL of redis service.
:param url: URL of redis service.
:param port: Port number at which redis service is exposed. If not specified, uses port 6379 by default.
"""
self.__server = server
self.__server = url
self.__port = port
self.__client = redis.StrictRedis(host=server, port=port)
self.__client = redis.StrictRedis(host=url, port=port)
try:
self.__client.ping()
except redis.ConnectionError as connection_error:
warnings.warn('Failed to connect to redis server: {0} at port: {1}'.format(server, port))
warnings.warn('Failed to connect to redis server: {0} at port: {1}'.format(url, port))
raise RedisBackendException(connection_error)

def is_client_valid(self):
Expand Down
2 changes: 1 addition & 1 deletion cache4py/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ def hash_key(python_object):
:param python_object: A python object.
:return: Consistent sha224 hash for the key_object.
"""
serialized_key = pickle.dumps(python_object)
serialized_key = pickle.dumps(python_object, protocol=pickle.HIGHEST_PROTOCOL)
hashed_key = sha224(serialized_key).hexdigest()
return hashed_key
169 changes: 169 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
"""
Author: nitin
Date: 19/10/18
Description: setup.py
"""

# !/usr/bin/env python
# -*- coding: utf-8 -*-

# Note: To use the 'upload' functionality of this file, you must:
# $ pip install twine

import io
import os
import sys
from shutil import rmtree

from setuptools import find_packages, setup, Command

# Package meta-data.
NAME = 'cache4py'
DESCRIPTION = 'Caching simplified.'
URL = 'https://github.com/nitinl/cache4py'
EMAIL = 'nitinlabhishetty@gmail.com'
AUTHOR = 'Nitin Labhishetty'
REQUIRES_PYTHON = '>=3.6.0'
VERSION = '0.0.1'

# What packages are required for this module to be executed?
REQUIRED = [
'redis', 'pymemcache',
]

# What packages are optional?
EXTRAS = {
'test': ['pytest', 'pytest-cov'],
}

# The rest you shouldn't have to touch too much :)
# ------------------------------------------------
# Except, perhaps the License and Trove Classifiers!
# If you do change the License, remember to change the Trove Classifier for that!

here = os.path.abspath(os.path.dirname(__file__))

# Import the README and use it as the long-description.
# Note: this will only work if 'README.md' is present in your MANIFEST.in file!
try:
with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = '\n' + f.read()
except FileNotFoundError:
long_description = DESCRIPTION

# Load the package's __version__.py module as a dictionary.
about = {}
if not VERSION:
with open(os.path.join(here, NAME, '__version__.py')) as f:
exec(f.read(), about)
else:
about['__version__'] = VERSION


class UploadCommand(Command):
"""Support setup.py upload."""

description = 'Build and publish the package.'
user_options = []

@staticmethod
def status(s):
"""Prints things in bold."""
print('\033[1m{0}\033[0m'.format(s))

def initialize_options(self):
pass

def finalize_options(self):
pass

def run(self):
try:
self.status('Removing previous builds…')
rmtree(os.path.join(here, 'dist'))
except OSError:
pass

self.status('Building Source and Wheel (universal) distribution…')
os.system('{0} setup.py sdist bdist_wheel --universal'.format(sys.executable))

self.status('Uploading the package to PyPI via Twine…')
os.system('twine upload dist/*')

self.status('Pushing git tags…')
os.system('git tag v{0}'.format(about['__version__']))
os.system('git push --tags')

sys.exit()

class TestUploadCommand(Command):
"""Support setup.py upload."""

description = 'Build and publish the package to test pypi.'
user_options = []

@staticmethod
def status(s):
"""Prints things in bold."""
print('\033[1m{0}\033[0m'.format(s))

def initialize_options(self):
pass

def finalize_options(self):
pass

def run(self):
try:
self.status('Removing previous builds…')
rmtree(os.path.join(here, 'dist'))
except OSError:
pass

self.status('Building Source and Wheel (universal) distribution…')
os.system('{0} setup.py sdist bdist_wheel --universal'.format(sys.executable))

self.status('Uploading the package to PyPI via Twine…')
os.system('twine upload --repository-url https://test.pypi.org/legacy/ dist/*')

sys.exit()


# Where the magic happens:
setup(
name=NAME,
version=about['__version__'],
description=DESCRIPTION,
long_description=long_description,
long_description_content_type='text/markdown',
author=AUTHOR,
author_email=EMAIL,
python_requires=REQUIRES_PYTHON,
url=URL,
packages=find_packages(exclude=('tests',)),
# If your package is a single module, use this instead of 'packages':
# py_modules=['mypackage'],

# entry_points={
# 'console_scripts': ['mycli=mymodule:cli'],
# },
install_requires=REQUIRED,
extras_require=EXTRAS,
include_package_data=True,
license='MIT',
classifiers=[
# Trove classifiers
# Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
'License :: OSI Approved :: MIT License',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy'
],
# $ setup.py publish support.
cmdclass={
'upload': UploadCommand,
'test_upload': TestUploadCommand,
},
)
2 changes: 1 addition & 1 deletion tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_redis():

start_time = time.time()
for i in range(5):
_ = redis_target_function(75000)
_ = redis_target_function(75000)
cached_time = time.time() - start_time

print("Time difference: before: {0}, after: {1}".format(uncached_time, cached_time))
Expand Down