-
-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Apache Ignite backend support #254
base: master
Are you sure you want to change the base?
Changes from 1 commit
b6049d8
12df0e1
5b2caad
127e374
e767005
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .ignitecache import IgniteCache | ||
|
||
__all__ = ['IgniteCache'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to only use the full important path for contributed caches -- without importing the cache class here. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
flask_caching.contrib.ignite | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The Apache Ignite caching backend. | ||
|
||
:copyright: (c) 2021 by Stephen Darlington, GridGain System. | ||
:license: BSD, see LICENSE for more details. | ||
""" | ||
import platform | ||
|
||
from flask_caching.backends.base import BaseCache | ||
|
||
try: | ||
import cPickle as pickle | ||
except ImportError: # pragma: no cover | ||
import pickle # type: ignore | ||
|
||
try: | ||
from pyignite import Client | ||
except ImportError: | ||
raise RuntimeError("no pyignite module found") | ||
|
||
|
||
class IgniteCache(BaseCache): | ||
"""Implements the cache using Apache Ignite. | ||
|
||
:param default_timeout: The default timeout in seconds. | ||
:param cache: The name of the caching to create or connect to, defaults to FLASK_CACHE | ||
:param hosts: host and port to connect to, defaults to localhost:10800 | ||
""" | ||
|
||
def __init__(self, default_timeout=300, cache="FLASK_CACHE", hosts="localhost:10800"): | ||
super(IgniteCache, self).__init__(default_timeout) | ||
|
||
self.client = Client() | ||
host_components = hosts.split(':') | ||
self.client.connect(host_components[0], int(host_components[1])) | ||
self.cache = self.client.get_or_create_cache(cache) | ||
|
||
@classmethod | ||
def factory(cls, app, config, args, kwargs): | ||
ignite_hosts = config.get("IGNITE_HOSTS","localhost:10800") | ||
ignite_cache_name = config.get("CACHE_IGNITE_NAME", "FLASK_CACHE") | ||
kwargs.update(dict(cache=ignite_cache_name, hosts=ignite_hosts)) | ||
return cls(*args, **kwargs) | ||
|
||
def get(self, key): | ||
rv = self.cache.get(key) | ||
if rv is None: | ||
return | ||
return pickle.loads(rv) | ||
|
||
def delete(self, key): | ||
return self.cache.remove_key(key) | ||
|
||
def set(self, key, value, timeout=None): | ||
if timeout is None: | ||
cache = self.cache | ||
else: | ||
cache = self.cache.with_expire_policy(access=timeout) | ||
cache.put( | ||
key, | ||
pickle.dumps(value), | ||
) | ||
|
||
def add(self, key, value, timeout=None): | ||
if timeout is None: | ||
cache = self.cache | ||
else: | ||
cache = self.cache.with_expire_policy(access=timeout) | ||
cache.put( | ||
key, | ||
pickle.dumps(value), | ||
) | ||
|
||
def clear(self): | ||
return self.cache.remove_all() | ||
|
||
def has(self, key): | ||
return self.cache.contains_key(key) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
SASLMemcachedCache, | ||
SimpleCache, | ||
SpreadSASLMemcachedCache, | ||
IgniteCache, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't provide tests in the official testsuite for use contributed backends. |
||
) | ||
|
||
|
||
|
@@ -32,6 +33,7 @@ def app(): | |
SASLMemcachedCache, | ||
SimpleCache, | ||
SpreadSASLMemcachedCache, | ||
IgniteCache, | ||
), | ||
) | ||
def test_init_nullcache(cache_type, app, tmp_path): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't include your backend here. This is only for officially supported backends and not for user contributed backends.