Squash out the now-redundant ApplicationServicesCache object class
parent
55022d6ca5
commit
4631b737fd
|
@ -23,23 +23,11 @@ from ._base import SQLBaseStore
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ApplicationServiceCache(object):
|
|
||||||
"""Caches ApplicationServices and provides utility functions on top.
|
|
||||||
|
|
||||||
This class is designed to be invoked on incoming events in order to avoid
|
|
||||||
hammering the database every time to extract a list of application service
|
|
||||||
regexes.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self.services = []
|
|
||||||
|
|
||||||
|
|
||||||
class ApplicationServiceStore(SQLBaseStore):
|
class ApplicationServiceStore(SQLBaseStore):
|
||||||
|
|
||||||
def __init__(self, hs):
|
def __init__(self, hs):
|
||||||
super(ApplicationServiceStore, self).__init__(hs)
|
super(ApplicationServiceStore, self).__init__(hs)
|
||||||
self.cache = ApplicationServiceCache()
|
self.services_cache = []
|
||||||
self.cache_defer = self._populate_cache()
|
self.cache_defer = self._populate_cache()
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
|
@ -56,7 +44,7 @@ class ApplicationServiceStore(SQLBaseStore):
|
||||||
token,
|
token,
|
||||||
)
|
)
|
||||||
# update cache TODO: Should this be in the txn?
|
# update cache TODO: Should this be in the txn?
|
||||||
for service in self.cache.services:
|
for service in self.services_cache:
|
||||||
if service.token == token:
|
if service.token == token:
|
||||||
service.url = None
|
service.url = None
|
||||||
service.namespaces = None
|
service.namespaces = None
|
||||||
|
@ -110,13 +98,13 @@ class ApplicationServiceStore(SQLBaseStore):
|
||||||
)
|
)
|
||||||
|
|
||||||
# update cache TODO: Should this be in the txn?
|
# update cache TODO: Should this be in the txn?
|
||||||
for (index, cache_service) in enumerate(self.cache.services):
|
for (index, cache_service) in enumerate(self.services_cache):
|
||||||
if service.token == cache_service.token:
|
if service.token == cache_service.token:
|
||||||
self.cache.services[index] = service
|
self.services_cache[index] = service
|
||||||
logger.info("Updated: %s", service)
|
logger.info("Updated: %s", service)
|
||||||
return
|
return
|
||||||
# new entry
|
# new entry
|
||||||
self.cache.services.append(service)
|
self.services_cache.append(service)
|
||||||
logger.info("Updated(new): %s", service)
|
logger.info("Updated(new): %s", service)
|
||||||
|
|
||||||
def _update_app_service_txn(self, txn, service):
|
def _update_app_service_txn(self, txn, service):
|
||||||
|
@ -160,7 +148,7 @@ class ApplicationServiceStore(SQLBaseStore):
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def get_app_services(self):
|
def get_app_services(self):
|
||||||
yield self.cache_defer # make sure the cache is ready
|
yield self.cache_defer # make sure the cache is ready
|
||||||
defer.returnValue(self.cache.services)
|
defer.returnValue(self.services_cache)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def get_app_service_by_token(self, token, from_cache=True):
|
def get_app_service_by_token(self, token, from_cache=True):
|
||||||
|
@ -176,7 +164,7 @@ class ApplicationServiceStore(SQLBaseStore):
|
||||||
yield self.cache_defer # make sure the cache is ready
|
yield self.cache_defer # make sure the cache is ready
|
||||||
|
|
||||||
if from_cache:
|
if from_cache:
|
||||||
for service in self.cache.services:
|
for service in self.services_cache:
|
||||||
if service.token == token:
|
if service.token == token:
|
||||||
defer.returnValue(service)
|
defer.returnValue(service)
|
||||||
return
|
return
|
||||||
|
@ -235,7 +223,7 @@ class ApplicationServiceStore(SQLBaseStore):
|
||||||
# TODO get last successful txn id f.e. service
|
# TODO get last successful txn id f.e. service
|
||||||
for service in services.values():
|
for service in services.values():
|
||||||
logger.info("Found application service: %s", service)
|
logger.info("Found application service: %s", service)
|
||||||
self.cache.services.append(ApplicationService(
|
self.services_cache.append(ApplicationService(
|
||||||
token=service["token"],
|
token=service["token"],
|
||||||
url=service["url"],
|
url=service["url"],
|
||||||
namespaces=service["namespaces"],
|
namespaces=service["namespaces"],
|
||||||
|
|
Loading…
Reference in New Issue