seitime-frappe/frappe/tests/test_client_cache.py
Ankush Menat b5e2569c0c feat: statistics for cache performance
These can be used for logging or debugging the behaviour of the cache.
2025-01-07 11:47:52 +05:30

69 lines
2.2 KiB
Python

import time
import frappe
from frappe.tests import IntegrationTestCase
from frappe.utils.redis_wrapper import ClientCache
TEST_KEY = "42"
class TestClientCache(IntegrationTestCase):
def setUp(self) -> None:
frappe.client_cache.delete_value(TEST_KEY)
return super().setUp()
def test_client_cache_is_used(self):
frappe.client_cache.set_value(TEST_KEY, 42)
frappe.client_cache.get_value(TEST_KEY)
with self.assertRedisCallCounts(0):
frappe.client_cache.get_value(TEST_KEY)
def test_client_cache_is_updated_instantly_noloop(self):
val = frappe.generate_hash()
frappe.client_cache.set_value(TEST_KEY, val)
with self.assertRedisCallCounts(0): # Locally set value should not be invalidated.
self.assertEqual(frappe.client_cache.get_value(TEST_KEY), val)
def test_invalidation_from_another_client_works(self):
frappe.client_cache.reset_statistics()
val = frappe.generate_hash()
frappe.client_cache.set_value(TEST_KEY, val)
self.assertEqual(frappe.client_cache.get_value(TEST_KEY), val)
# frappe.cache is our "another client"
val = frappe.generate_hash()
frappe.cache.set_value(TEST_KEY, val)
# This is almost instant, but obviously not as fast as running the next instruction in
# current thread. So we wait.
time.sleep(0.1)
with self.assertRedisCallCounts(1, exact=True):
self.assertEqual(frappe.client_cache.get_value(TEST_KEY), val)
self.assertEqual(frappe.client_cache.statistics.hits, 1)
self.assertEqual(frappe.client_cache.statistics.misses, 1)
self.assertEqual(frappe.client_cache.statistics.hit_ratio, 0.5)
def test_client_local_cache_ttl(self):
c = ClientCache(ttl=1)
c.set_value(TEST_KEY, 42)
with self.assertRedisCallCounts(0):
c.get_value(TEST_KEY)
time.sleep(1)
with self.assertRedisCallCounts(1, exact=True):
c.get_value(TEST_KEY)
def test_client_cache_maxsize(self):
c = ClientCache(maxsize=2)
c.set_value(TEST_KEY, 42)
c.set_value(frappe.generate_hash(), 42)
c.set_value(frappe.generate_hash(), 42)
self.assertEqual(len(c.cache), 2)
def test_shared_keyspace(self):
val = frappe.generate_hash()
frappe.client_cache.set_value(TEST_KEY, val)
self.assertEqual(frappe.client_cache.get_value(TEST_KEY), frappe.cache.get_value(TEST_KEY))