comparison libervia/server/pages.py @ 1490:774a81a6e8b5

pages: new `useCache` method to check if `no-cache` is used in request
author Goffi <goffi@goffi.org>
date Wed, 26 Jan 2022 10:23:07 +0100
parents 095e94ca6728
children 1671d187e71d
comparison
equal deleted inserted replaced
1489:ff23865fbdbb 1490:774a81a6e8b5
1071 # FIXME: cache must be marked as unusable here 1071 # FIXME: cache must be marked as unusable here
1072 1072
1073 def psNodeWatchAddEb(self, failure_, service, node): 1073 def psNodeWatchAddEb(self, failure_, service, node):
1074 log.warning(_("Can't add node watched: {msg}").format(msg=failure_)) 1074 log.warning(_("Can't add node watched: {msg}").format(msg=failure_))
1075 1075
1076 def useCache(self, request: server.Request) -> bool:
1077 """Indicate if the cache should be used
1078
1079 test request header to see if it is requested to skip the cache
1080 @return: True if cache should be used
1081 """
1082 return request.getHeader('cache-control') != 'no-cache'
1083
1076 def checkCache(self, request, cache_type, **kwargs): 1084 def checkCache(self, request, cache_type, **kwargs):
1077 """check if a page is in cache and return cached version if suitable 1085 """check if a page is in cache and return cached version if suitable
1078 1086
1079 this method may perform extra operation to handle cache (e.g. subscribing to a 1087 this method may perform extra operation to handle cache (e.g. subscribing to a
1080 pubsub node) 1088 pubsub node)
1094 if request.uri != request.path: 1102 if request.uri != request.path:
1095 # we don't cache page with query arguments as there can be a lot of variants 1103 # we don't cache page with query arguments as there can be a lot of variants
1096 # influencing page results (e.g. search terms) 1104 # influencing page results (e.g. search terms)
1097 log.debug("ignoring cache due to query arguments") 1105 log.debug("ignoring cache due to query arguments")
1098 1106
1099 no_cache = request.getHeader('cache-control') == 'no-cache' 1107 no_cache = not self.useCache(request)
1100 1108
1101 profile = self.getProfile(request) or C.SERVICE_PROFILE 1109 profile = self.getProfile(request) or C.SERVICE_PROFILE
1102 1110
1103 if cache_type == C.CACHE_PUBSUB: 1111 if cache_type == C.CACHE_PUBSUB:
1104 service, node = kwargs["service"], kwargs["node"] 1112 service, node = kwargs["service"], kwargs["node"]