Skip to content
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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions flask_caching/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
)
from flask_caching.backends.simplecache import SimpleCache
from flask_caching.backends.uwsgicache import UWSGICache
from flask_caching.contrib.ignitecache import IgniteCache
Copy link
Collaborator

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.



__all__ = (
Expand All @@ -39,6 +40,7 @@
"gaememcached",
"saslmemcached",
"spreadsaslmemcached",
"ignite",
)


Expand Down Expand Up @@ -84,3 +86,7 @@ def saslmemcached(app, config, args, kwargs):

def spreadsaslmemcached(app, config, args, kwargs):
return SpreadSASLMemcachedCache.factory(app, config, args, kwargs)

def ignite(app, config, args, kwargs):
return IgniteCache.factory(app, config, args, kwargs)

3 changes: 3 additions & 0 deletions flask_caching/contrib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .ignitecache import IgniteCache

__all__ = ['IgniteCache']
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

82 changes: 82 additions & 0 deletions flask_caching/contrib/ignitecache.py
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)
2 changes: 2 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
SASLMemcachedCache,
SimpleCache,
SpreadSASLMemcachedCache,
IgniteCache,
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

)


Expand All @@ -32,6 +33,7 @@ def app():
SASLMemcachedCache,
SimpleCache,
SpreadSASLMemcachedCache,
IgniteCache,
),
)
def test_init_nullcache(cache_type, app, tmp_path):
Expand Down