├── .gitignore ├── LICENSE ├── README.md ├── keydb ├── __init__.py └── client.py ├── setup.py └── tests ├── __init__.py └── cmd.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018 The Python Packaging Authority 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # keydb-python 2 | 3 | A python client for [KeyDB](https://keydb.dev/) derived from [redis-py](https://github.com/andymccurdy/redis-py). You can use all classes from `redis` package as `keydb`. For example to use connection pool 4 | 5 | ```python 6 | from keydb import KeyDB 7 | from keydb import ConnectionPool 8 | 9 | pool = ConnectionPool(host='localhost', password='password') 10 | db = KeyDB(connection_pool=pool) 11 | ``` 12 | 13 | 14 | #### Examples 15 | 16 | ```python 17 | import time 18 | from keydb import KeyDB 19 | 20 | db = KeyDB(host='localhost', password='password') 21 | 22 | db.set("A", 10) 23 | 24 | db.sadd("SS", 11, 12, 13, 14, 15) 25 | db.scard("SS") 26 | 27 | db.expiremember("SS", 12, 5) 28 | time.sleep(6) 29 | 30 | db.scard("SS") 31 | ``` 32 | 33 | 34 | #### Contribution 35 | You can add new commands from KeyDB CLI in this repository. Another related to redis, please contribute to [redis-py](https://github.com/andymccurdy/redis-py) -------------------------------------------------------------------------------- /keydb/__init__.py: -------------------------------------------------------------------------------- 1 | from keydb.client import * 2 | -------------------------------------------------------------------------------- /keydb/client.py: -------------------------------------------------------------------------------- 1 | from redis import * 2 | 3 | 4 | class KeyDB(StrictRedis): 5 | 6 | def __init__(self, **kwargs): 7 | super(KeyDB, self).__init__(**kwargs) 8 | 9 | def expiremember(self, key, subkey, delay, unit=None): 10 | """ 11 | Set timeout on a subkey. This feature only available on KeyDB 12 | https://docs.keydb.dev/docs/commands/#expiremember 13 | :param key: key added by `SADD key [subkeys]` 14 | :param subkey: subkey on the set 15 | :param delay: timeout 16 | :param unit: `s` or `ms` 17 | :return: 0 if the timeout was set, otherwise 0 18 | """ 19 | args = [key, subkey, delay] 20 | if unit is not None and unit not in ['s', 'ms']: 21 | raise ValueError("`unit` must be s or ms") 22 | 23 | if unit: 24 | args.append(unit) 25 | 26 | return self.execute_command('EXPIREMEMBER', *args) 27 | 28 | def expirememberat(self, key, subkey, timestamp): 29 | """ 30 | Set timeout on a subkey by timestamp instead of seconds 31 | https://docs.keydb.dev/docs/commands/#expirememberat 32 | :param key: 33 | :param subkey: 34 | :param timestamp: 35 | :return: 36 | """ 37 | return self.execute_command('EXPIREMEMBERAT', key, subkey, timestamp) 38 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import setuptools 2 | 3 | with open("README.md", "r") as fh: 4 | des = fh.read() 5 | 6 | setuptools.setup( 7 | name="keydb", 8 | version="0.0.1", 9 | author="Example Author", 10 | author_email="kimnt93@gmail.com", 11 | description="A Python client for KeyDB", 12 | long_description=des, 13 | long_description_content_type="text/markdown", 14 | url="https://github.com/dpsnewailab/keydb-python", 15 | packages=setuptools.find_packages(), 16 | classifiers=[ 17 | "License :: OSI Approved :: MIT License", 18 | "Operating System :: OS Independent", 19 | "Programming Language :: Python", 20 | "Programming Language :: Python :: 2", 21 | "Programming Language :: Python :: 2.7", 22 | "Programming Language :: Python :: 3", 23 | "Programming Language :: Python :: 3.5", 24 | "Programming Language :: Python :: 3.6", 25 | "Programming Language :: Python :: 3.7", 26 | "Programming Language :: Python :: 3.8" 27 | ], 28 | python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*', 29 | install_requires=[ 30 | 'redis' 31 | ] 32 | ) 33 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dpsnewailab/keydb-python/589ad851292f1e4140dc238629ff2a56e089f402/tests/__init__.py -------------------------------------------------------------------------------- /tests/cmd.py: -------------------------------------------------------------------------------- 1 | from keydb import * 2 | import time 3 | 4 | 5 | if __name__ == "__main__": 6 | 7 | pool = ConnectionPool(host='localhost', password='password') 8 | db = KeyDB(connection_pool=pool) 9 | 10 | db.set("A", 10) 11 | 12 | db.sadd("SS", 11, 12, 13, 14, 15) 13 | print(f'Set size: {db.scard("SS")}') 14 | 15 | db.expiremember("SS", 12, 5) 16 | time.sleep(6) 17 | 18 | print(f'Set size after 6 seconds: {db.scard("SS")}') 19 | --------------------------------------------------------------------------------