11import logging
22import sys
3- import datetime
3+ from datetime import datetime
4+ from datetime import timedelta
5+
46from requests import HTTPError
57
8+ from . import utils
9+ from .configfetcher import FetchResponse
610from .readwritelock import ReadWriteLock
711from .interfaces import CachePolicy
812
@@ -16,22 +20,23 @@ def __init__(self, config_fetcher, config_cache, cache_key, cache_time_to_live_s
1620 self ._config_fetcher = config_fetcher
1721 self ._config_cache = config_cache
1822 self ._cache_key = cache_key
19- self ._cache_time_to_live = datetime . timedelta (seconds = cache_time_to_live_seconds )
23+ self ._cache_time_to_live = timedelta (seconds = cache_time_to_live_seconds )
2024 self ._lock = ReadWriteLock ()
21- self ._last_updated = None
2225
2326 def get (self ):
24- config = None
27+ configuration = None
28+ etag = ''
2529
2630 try :
2731 self ._lock .acquire_read ()
2832
29- config = self ._config_cache .get (self ._cache_key )
33+ configuration = self ._config_cache .get (self ._cache_key )
34+
35+ utc_now = utils .get_utc_now ()
3036
31- utc_now = datetime .datetime .utcnow ()
32- if self ._last_updated is not None and self ._last_updated + self ._cache_time_to_live > utc_now :
33- if config is not None :
34- return config
37+ if configuration is not None :
38+ if datetime .utcfromtimestamp (configuration .get (FetchResponse .FETCH_TIME , 0 )) + self ._cache_time_to_live > utc_now :
39+ return configuration .get (FetchResponse .CONFIG )
3540 finally :
3641 self ._lock .release_read ()
3742
@@ -40,43 +45,43 @@ def get(self):
4045 # If while waiting to acquire the write lock another
4146 # thread has updated the content, then don't bother requesting
4247 # to the server to minimise time.
43- if config is None or self ._last_updated is None or self ._last_updated + self ._cache_time_to_live <= datetime .datetime .utcnow ():
44- force_fetch = not bool (config )
45- self ._force_refresh (force_fetch )
48+ utc_now = utils .get_utc_now ()
49+ if configuration is None or datetime .utcfromtimestamp (configuration .get (FetchResponse .FETCH_TIME , 0 )) + self ._cache_time_to_live <= utc_now :
50+ if bool (configuration ):
51+ etag = configuration .get (FetchResponse .ETAG , '' )
52+ self ._force_refresh (etag )
4653 finally :
4754 self ._lock .release_write ()
4855
4956 try :
5057 self ._lock .acquire_read ()
51- config = self ._config_cache .get (self ._cache_key )
52- return config
58+ configuration = self ._config_cache .get (self ._cache_key )
59+ return configuration . get ( FetchResponse . CONFIG ) if configuration else None
5360 finally :
5461 self ._lock .release_read ()
5562
56- def force_refresh (self ):
57- force_fetch = False
58-
63+ def force_refresh (self , etag = '' ):
5964 try :
6065 self ._lock .acquire_read ()
61- config = self ._config_cache .get (self ._cache_key )
62- force_fetch = not bool (config )
66+ configuration = self ._config_cache .get (self ._cache_key )
67+ if bool (configuration ):
68+ etag = configuration .get (FetchResponse .ETAG , '' )
6369 finally :
6470 self ._lock .release_read ()
6571
6672 try :
6773 self ._lock .acquire_write ()
68- self ._force_refresh (force_fetch )
74+ self ._force_refresh (etag )
6975 finally :
7076 self ._lock .release_write ()
7177
72- def _force_refresh (self , force_fetch ):
78+ def _force_refresh (self , etag ):
7379 try :
74- configuration_response = self ._config_fetcher .get_configuration_json (force_fetch )
75- # set _last_updated regardless of whether the cache is updated
80+ configuration_response = self ._config_fetcher .get_configuration_json (etag )
81+ # set _config_cache regardless of whether the cache is updated
7682 # or whether a 304 not modified has been sent back as the content
7783 # we have hasn't been updated on the server so not need
7884 # for subsequent requests to retry this within the cache time to live
79- self ._last_updated = datetime .datetime .utcnow ()
8085 if configuration_response .is_fetched ():
8186 configuration = configuration_response .json ()
8287 self ._config_cache .set (self ._cache_key , configuration )
0 commit comments