def setUp(self): super().setUp() # installs new, empty cache instance for each test install_schema_fetcher_cache() for cache in caches.all(): cache.clear()
def _close_django_caches(self, shutdown=False): if self.use_django and django_caches: if shutdown: self.logger.info('Closing all Django caches') for cache in django_caches.all(): cache.close(for_shutdown=shutdown)
def readiness(self, request): # Connect to each database and do a generic standard SQL query # that doesn't write any data and doesn't depend on any tables # being present. try: requests.get(settings.HSD_URI, timeout=5) except Exception as e: logger.exception(e) return HttpResponseServerError("db: cannot connect to hsd.") # Call get_stats() to connect to each memcached instance and get it's stats. # This can effectively check if each is online. try: from django.core.cache import caches from django.core.cache.backends.memcached import BaseMemcachedCache for cache in caches.all(): if isinstance(cache, BaseMemcachedCache): stats = cache._cache.get_stats() if len(stats) != len(cache._servers): return HttpResponseServerError("cache: cannot connect to cache.") except Exception as e: logger.exception(e) return HttpResponseServerError("cache: cannot connect to cache.") return HttpResponse("OK")
def handle(self, *args, **options): self.stdout.write("\nClear caches:\n") for cache in caches.all(): self.stdout.write("\tClear '%s'\n" % cache.__class__.__name__) cache.clear() self.stdout.write("\ndone.\n")
def readyz(self, request): """Returns that the databases and caches are ready""" # Connect to each database and do a generic standard SQL query that doesn't # write any data and doesn't depend on any tables being present. try: from django.db import connections for name in connections: cursor = connections[name].cursor() cursor.execute("SELECT 1;") row = cursor.fetchone() if row is None: return HttpResponseServerError("db: invalid response") except Exception as e: LOGGER.exception(e) return HttpResponseServerError("db: cannot connect to database.") # Call get_stats() to connect to each memcached instance and get it's stats. # This can effectively check if each is online. try: from django.core.cache import caches from django.core.cache.backends.memcached import BaseMemcachedCache for cache in caches.all(): if isinstance(cache, BaseMemcachedCache): stats = cache._cache.get_stats() if len(stats) != len(cache._servers): return HttpResponseServerError( "cache: cannot connect to cache.") except Exception as e: LOGGER.exception(e) return HttpResponseServerError("cache: cannot connect to cache.") return HttpResponse("OK")
def readiness(self, request): """ Metódo para realizar teste de conexão com o banco de dados """ try: for name in connections: cursor = connections[name].cursor() cursor.execute("SELECT 1;") row = cursor.fetchone() if row is None: return HttpResponseServerError( "Banco de dados: erro de banco de dados.") except (Exception) as erro_banco: LOGGER.exception(erro_banco) return HttpResponseServerError( "Banco de dados: não foi possível conectar.") # Chama o metódo get_stats() para conectar em todas as instâncias cache do Django # e verifica o status. # Se todas as instâncias estiverem OK, o Django está em pleno funcionamento try: for cache in caches.all(): if isinstance(cache, BaseMemcachedCache): stats = cache._cache.get_stats() if len(stats) != len(cache._servers): return HttpResponseServerError("Cache: erro de cache.") except (Exception) as erro_cache: LOGGER.exception(erro_cache) return HttpResponseServerError( "Cache: não foi possível conectar ao cache.") # Retorna uma confirmação de OK para o protocolo HTTP return HttpResponse("OK")
def readiness(self, request): # Connect to each database and do a generic standard SQL query # that doesn't write any data and doesn't depend on any tables # being present. newrelic.agent.ignore_transaction(flag=True) try: from django.db import connections for name in connections: cursor = connections[name].cursor() cursor.execute('SELECT 1;') row = cursor.fetchone() if row is None: return HttpResponseServerError('db: invalid response') except Exception as e: logger.exception(e) return HttpResponseServerError('db: cannot connect to database.') # Call get_stats() to connect to each memcached instance and get it's stats. # This can effectively check if each is online. try: from django.core.cache import caches from django.core.cache.backends.memcached import BaseMemcachedCache for cache in caches.all(): if isinstance(cache, BaseMemcachedCache): stats = cache._cache.get_stats() if len(stats) != len(cache._servers): return HttpResponseServerError( 'cache: cannot connect to cache.') except Exception as e: logger.exception(e) return HttpResponseServerError('cache: cannot connect to cache.') return HttpResponse('OK')
class HealthCheckMiddleware(object): def __init__(self, get_response=None): self.get_response = get_response # One-time configuration and initialization. def process_request(self, request): if request.method == "GET": if request.path == "/_status/readiness": return self.readiness(request) elif request.path == "/_status/healthz": return self.healthz(request) def __call__(self, request): if self.get_response: self.process_request(request) return self.get_response(request) else: raise RuntimeError( "No get_response callable given. Callable middleware not supported." ) def healthz(self, request): """ Returns that the server is alive. """ return HttpResponse("OK") def readiness(self, request): # Connect to each database and do a generic standard SQL query # that doesn't write any data and doesn't depend on any tables # being present. try: from django.db import connections for name in connections: cursor = connections[name].cursor() cursor.execute("SELECT 1;") row = cursor.fetchone() if row is None: return HttpResponseServerError("db: invalid response") except Exception, e: logger.exception(e) return HttpResponseServerError("db: cannot connect to database.") # Call get_stats() to connect to each memcached instance and get it's stats. # This can effectively check if each is online. try: from django.core.cache import caches from django.core.cache.backends.memcached import BaseMemcachedCache for cache in caches.all(): if isinstance(cache, BaseMemcachedCache): stats = cache._cache.get_stats() if len(stats) != len(cache._servers): return HttpResponseServerError( "cache: cannot connect to cache.") except Exception, e: logger.exception(e) return HttpResponseServerError("cache: cannot connect to cache.")
def patchInitializedCaches(lib): from django.core.cache import caches cache_classes = caches.all() for cache_class in cache_classes: cache_class._lib = lib if hasattr(cache_class, '_client'): client_kwargs = dict(pickleProtocol=pickle.HIGHEST_PROTOCOL) client_kwargs.update(cache_class._options) cache_class._client = lib.Client( cache_class._servers, **client_kwargs)
def instrument(self): from django.core.cache import caches cache_classes = caches.all() for cache_class in cache_classes: cache_class._lib = Library if hasattr(cache_class, '_client'): client_kwargs = dict(pickleProtocol=pickle.HIGHEST_PROTOCOL) client_kwargs.update(cache_class._options) cache_class._client = ScoobyMemcacheClient( cache_class._servers, **client_kwargs) super(MemcachePlugin, self).instrument()
def enable_instrumentation(self): # Monkey patch all open cache connections. Django maintains cache connections # on a per-thread/async task basis, so this will not affect any concurrent # requests. The monkey patch of CacheHander.create_connection() installed in # the .ready() method will ensure that any new cache connections that get opened # during this request will also be monkey patched. for cache in caches.all(initialized_only=True): self._monkey_patch_cache(cache) # Mark this panel instance as the current one for the active thread/async task # context. This will be used by the CacheHander.create_connection() monkey # patch. self._context_locals.current_instance = self
def healthz(self, request): """Return that the server is healthy.""" # Connect to each database and do a generic standard SQL query # that doesn't write any data and doesn't depend on any tables # being present. try: from django.db import connections for name in connections: cursor = connections[name].cursor() cursor.execute("SELECT 1;") row = cursor.fetchone() if row is None: return self._response(ko="db: invalid response") except Exception as e: self.logger.exception(e) return self._response(ko="db: cannot connect to database") # Do a roundtrip SET and GET on a random key/value. # This can effectively check if each is online. try: from django.core.cache import caches caches["default"] if not caches.all(): return self._response(ko="cache: no cache configured") for cache in caches.all(): cache_value = uuid.uuid4().hex cache.set(self.cache_key, cache_value) result = cache.get(self.cache_key) if result != cache_value: return self._response(ko="cache: round trip error") except Exception as e: self.logger.exception(e) return self._response(ko="cache: cannot connect to cache") return self._response()
def django_cache_add_xdist_key_prefix(request): skip_if_no_django() xdist_prefix = getattr(request.config, 'workerinput', {}).get('workerid') if xdist_prefix: # Put a prefix like gw0_, gw1_ etc on xdist processes for existing_cache in caches.all(): existing_cache.key_prefix = xdist_prefix + '_' + existing_cache.key_prefix existing_cache.clear() logger.info('Set existing cache key prefix to [%s]', existing_cache.key_prefix) for name, cache_settings in settings.CACHES.items(): cache_settings['KEY_PREFIX'] = xdist_prefix + '_' + cache_settings.get('KEY_PREFIX', '') logger.info('Set cache key prefix for [%s] cache to [%s]', name, cache_settings['KEY_PREFIX'])
def classes(request): print(caches.all()) print("Cache is") print(cache.has_key('%sall_classes' % (cache.key_prefix))) if cache.get('all_classes') == None: print("NOT using cache") classes = requests.get(BASE_PATH + CLASSES_PATH).json() all_classes = [] for d3_class in classes['results']: print(d3_class['url']) class_details = requests.get(BASE_PATH + d3_class['url']) all_classes.append(class_details.json()) cache.set('all_classes', all_classes, 86400) print(cache.key_prefix) else: print("using cache") all_classes = cache.get('all_classes') return render(request, 'classes.html', {'classes': all_classes})
def _clear_caches(self): for cache in caches.all(): cache.clear()
def cache_clear(): cache.clear() # Default one for c in caches.all(): c.clear()
def handle(self, *args, **options): for c in caches.all(): c.clear() exit(0)
def django_caches(): yield caches # finalizer for cache in caches.all(): cache.clear()
def clear_caches(): for cache in caches.all(): cache.clear()
def check_caches(self, key): """Check that caches are empty, and add values.""" for cache in caches.all(): assert cache.get(key) is None cache.set(key, "Not None")
def all(cls): return caches.all()
def clear_caches(): from django.core.cache import caches for cache in caches.all(): cache.clear()
def disable_instrumentation(self): if hasattr(self._context_locals, "current_instance"): del self._context_locals.current_instance for cache in caches.all(initialized_only=True): self._unmonkey_patch_cache(cache)
async def _close_async_caches(): for cache in caches.all(): await cache.close_async()
def clear_caches(request): for existing_cache in caches.all(): existing_cache.clear()
def check_caches(self, key): """Check that caches are empty, and add values.""" for cache in caches.all(): self.assertIsNone(cache.get(key)) cache.set(key, "Not None")