changeset 3ba37e19ec52 in modules/account_payment_stripe:default
details: 
https://hg.tryton.org/modules/account_payment_stripe?cmd=changeset;node=3ba37e19ec52
description:
        Add cache for sources

        issue8247
        review265221002
diffstat:

 CHANGELOG     |   1 +
 doc/index.rst |   9 +++++++++
 payment.py    |  16 ++++++++++++++++
 3 files changed, 26 insertions(+), 0 deletions(-)

diffs (78 lines):

diff -r fd06f3538108 -r 3ba37e19ec52 CHANGELOG
--- a/CHANGELOG Mon Apr 08 10:18:49 2019 +0200
+++ b/CHANGELOG Thu Apr 11 18:50:54 2019 +0200
@@ -1,3 +1,4 @@
+* Add cache for sources
 * Use queue to charge and capture
 
 Version 5.0.0 - 2018-10-01
diff -r fd06f3538108 -r 3ba37e19ec52 doc/index.rst
--- a/doc/index.rst     Mon Apr 08 10:18:49 2019 +0200
+++ b/doc/index.rst     Thu Apr 11 18:50:54 2019 +0200
@@ -45,3 +45,12 @@
 the amount in a second step.
 
 If the payment is disputed, it will be updated at the closure of the dispute.
+
+Configuration
+*************
+
+The account_payment_stripe module uses the section `account_payment_stripe` to
+retrieve some parameters:
+
+- `sources_cache`: defines the duration in seconds the sources are kept in the
+  cache. The default value is `15 * 60`.
diff -r fd06f3538108 -r 3ba37e19ec52 payment.py
--- a/payment.py        Mon Apr 08 10:18:49 2019 +0200
+++ b/payment.py        Thu Apr 11 18:50:54 2019 +0200
@@ -9,6 +9,8 @@
 
 import stripe
 
+from trytond.cache import Cache
+from trytond.config import config
 from trytond.i18n import gettext
 from trytond.model import (
     ModelSQL, ModelView, Workflow, DeactivableMixin, fields)
@@ -758,6 +760,11 @@
             'invisible': ~Eval('stripe_error_param'),
             })
 
+    _sources_cache = Cache(
+        'account_payment_stripe_customer.sources',
+        duration=config.getint(
+            'account_payment_stripe', 'sources_cache', default=15 * 60))
+
     @classmethod
     def __setup__(cls):
         super(Customer, cls).__setup__()
@@ -776,6 +783,11 @@
         return self.stripe_customer_id if self.stripe_customer_id else name
 
     @classmethod
+    def write(cls, *args, **kwargs):
+        super().write(*args, **kwargs)
+        cls._sources_cache.clear()
+
+    @classmethod
     def delete(cls, customers):
         cls.write(customers, {
                 'active': False,
@@ -887,6 +899,9 @@
             logger.warning(str(e))
 
     def sources(self):
+        sources = self._sources_cache.get(self.id)
+        if sources is not None:
+            return sources
         sources = []
         customer = self.retrieve()
         if customer:
@@ -901,6 +916,7 @@
                 else:
                     continue
                 sources.append((source.id, name))
+            self._sources_cache.set(self.id, sources)
         return sources
 
     def _source_name(cls, source):

Reply via email to