Skip to content

Commit

Permalink
feat: connect to redis sentinel for redis cache (#25398) (#25446)
Browse files Browse the repository at this point in the history
(cherry picked from commit 77618cd)

Co-authored-by: Revant Nandgaonkar <revant.one@gmail.com>
  • Loading branch information
mergify[bot] and revant committed Mar 15, 2024
1 parent d130777 commit 498b1a4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions frappe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,9 @@ def setup_redis_cache_connection():
global cache

if not cache:
from frappe.utils.redis_wrapper import RedisWrapper
from frappe.utils.redis_wrapper import setup_cache

cache = RedisWrapper.from_url(conf.get("redis_cache"))
cache = setup_cache()


def get_traceback(with_context: bool = False) -> str:
Expand Down
41 changes: 41 additions & 0 deletions frappe/utils/redis_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import redis
from redis.commands.search import Search
from redis.sentinel import Sentinel

import frappe
from frappe.utils import cstr
Expand Down Expand Up @@ -287,3 +288,43 @@ def smembers(self, name):

def ft(self, index_name="idx"):
return RedisearchWrapper(client=self, index_name=self.make_key(index_name))


def setup_cache():
if frappe.conf.redis_cache_sentinel_enabled:
sentinels = [tuple(node.split(":")) for node in frappe.conf.get("redis_cache_sentinels", [])]
sentinel = get_sentinel_connection(
sentinels=sentinels,
sentinel_username=frappe.conf.get("redis_cache_sentinel_username"),
sentinel_password=frappe.conf.get("redis_cache_sentinel_password"),
master_username=frappe.conf.get("redis_cache_master_username"),
master_password=frappe.conf.get("redis_cache_master_password"),
)
return sentinel.master_for(
frappe.conf.get("redis_cache_master_service"),
redis_class=RedisWrapper,
)

return RedisWrapper.from_url(frappe.conf.get("redis_cache"))


def get_sentinel_connection(
sentinels: list[tuple[str, int]],
sentinel_username=None,
sentinel_password=None,
master_username=None,
master_password=None,
):
sentinel_kwargs = {}
if sentinel_username:
sentinel_kwargs["username"] = sentinel_username

if sentinel_password:
sentinel_kwargs["password"] = sentinel_password

return Sentinel(
sentinels=sentinels,
sentinel_kwargs=sentinel_kwargs,
username=master_username,
password=master_password,
)

0 comments on commit 498b1a4

Please sign in to comment.