File manager - Edit - /opt/saltstack/salt/lib/python3.10/site-packages/salt/cache/__pycache__/redis_cache.cpython-310.pyc
Back
o ;j(U � @ s\ d Z ddlZddlZddlZddlZddlmZ zddlZddl m Z ddl mZ dZW n ey7 dZY nw z ddlmZ dZW n eyM dZY nw d Zd diZe�e�ZdZd ZdZdZdZdadd� Zdd� Zdd� Z d3dd�Z!dd� Z"dd� Z#dd� Z$dd � Z%d!d"� Z&d#d$� Z'd4d&d'�Z(d(d)� Z)d*d+� Z*d3d,d-�Z+d.d � Z,d/d0� Z-d1d2� Z.dS )5a Redis ===== Redis plugin for the Salt caching subsystem. .. versionadded:: 2017.7.0 .. versionchanged:: 3005 To enable this cache plugin, the master will need the python client for redis installed. This can be easily installed with pip: .. code-block:: bash salt \* pip.install redis As Redis provides a simple mechanism for very fast key-value store, in order to provide the necessary features for the Salt caching subsystem, the following conventions are used: - A Redis key consists of the bank name and the cache key separated by ``/``, e.g.: ``$KEY_minions/alpha/stuff`` where ``minions/alpha`` is the bank name and ``stuff`` is the key name. - As the caching subsystem is organised as a tree, we need to store the caching path and identify the bank and its offspring. At the same time, Redis is linear and we need to avoid doing ``keys <pattern>`` which is very inefficient as it goes through all the keys on the remote Redis server. Instead, each bank hierarchy has a Redis SET associated which stores the list of sub-banks. By default, these keys begin with ``$BANK_``. - In addition, each key name is stored in a separate SET of all the keys within a bank. By default, these SETs begin with ``$BANKEYS_``. For example, to store the key ``my-key`` under the bank ``root-bank/sub-bank/leaf-bank``, the following hierarchy will be built: .. code-block:: text 127.0.0.1:6379> SMEMBERS $BANK_root-bank 1) "sub-bank" 127.0.0.1:6379> SMEMBERS $BANK_root-bank/sub-bank 1) "leaf-bank" 127.0.0.1:6379> SMEMBERS $BANKEYS_root-bank/sub-bank/leaf-bank 1) "my-key" 127.0.0.1:6379> GET $KEY_root-bank/sub-bank/leaf-bank/my-key "my-value" There are four types of keys stored: - ``$BANK_*`` is a Redis SET containing the list of banks under the current bank. - ``$BANKEYS_*`` is a Redis SET containing the list of keys under the current bank. - ``$KEY_*`` keeps the value of the key. - ``$TSTAMP_*`` stores the last updated timestamp of the key. These prefixes and the separator can be adjusted using the configuration options: bank_prefix: ``$BANK`` The prefix used for the name of the Redis key storing the list of sub-banks. bank_keys_prefix: ``$BANKEYS`` The prefix used for the name of the Redis key storing the list of keys under a certain bank. key_prefix: ``$KEY`` The prefix of the Redis keys having the value of the keys to be cached under a certain bank. timestamp_prefix: ``$TSTAMP`` The prefix for the last modified timestamp for keys. .. versionadded:: 3005 separator: ``_`` The separator between the prefix and the key body. The connection details can be specified using: host: ``localhost`` The hostname of the Redis server. port: ``6379`` The Redis server port. cluster_mode: ``False`` Whether cluster_mode is enabled or not cluster.startup_nodes: A list of host, port dictionaries pointing to cluster members. At least one is required but multiple nodes are better .. code-block:: yaml cache.redis.cluster.startup_nodes - host: redis-member-1 port: 6379 - host: redis-member-2 port: 6379 cluster.skip_full_coverage_check: ``False`` Some cluster providers restrict certain redis commands such as CONFIG for enhanced security. Set this option to true to skip checks that required advanced privileges. .. note:: Most cloud hosted redis clusters will require this to be set to ``True`` db: ``'0'`` The database index. .. note:: The database index must be specified as string not as integer value! password: Redis connection password. unix_socket_path: .. versionadded:: 2018.3.1 Path to a UNIX socket for access. Overrides `host` / `port`. Configuration Example: .. code-block:: yaml cache.redis.host: localhost cache.redis.port: 6379 cache.redis.db: '0' cache.redis.password: my pass cache.redis.bank_prefix: #BANK cache.redis.bank_keys_prefix: #BANKEYS cache.redis.key_prefix: #KEY cache.redis.timestamp_prefix: #TICKS cache.redis.separator: '@' Cluster Configuration Example: .. code-block:: yaml cache.redis.cluster_mode: true cache.redis.cluster.skip_full_coverage_check: true cache.redis.cluster.startup_nodes: - host: redis-member-1 port: 6379 - host: redis-member-2 port: 6379 cache.redis.db: '0' cache.redis.password: my pass cache.redis.bank_prefix: #BANK cache.redis.bank_keys_prefix: #BANKEYS cache.redis.key_prefix: #KEY cache.redis.separator: '@' � N)�SaltCacheError)�ConnectionError)� ResponseErrorTF)�RedisCluster�redis�list_�listz$BANKz$KEYz$TSTAMPz$BANKEYS�_c C s t sdS ts t� d r dS tS )z� The redis library must be installed for this module to work. The redis redis cluster library must be installed if cluster_mode is True )Fz(Please install the python-redis package.�cluster_mode)Fz,Please install the redis-py-cluster package.)� HAS_REDIS�HAS_REDIS_CLUSTER�_get_redis_cache_opts�__virtualname__� r r �J/opt/saltstack/salt/lib/python3.10/site-packages/salt/cache/redis_cache.py�__virtual__� s r c C s i S )z9 Effectively a noop. Return an empty dictionary. r )�kwargsr r r �init_kwargs� s r c C sV t �dd�t �dd�t �dd�t �dd�t �d d �t �dd�t �d i �t �dd�d�S )zG Return the Redis server connection details from the __opts__. zcache.redis.hostZ localhostzcache.redis.porti� zcache.redis.unix_socket_pathNzcache.redis.db�0zcache.redis.password� zcache.redis.cluster_modeFz!cache.redis.cluster.startup_nodesz,cache.redis.cluster.skip_full_coverage_check)�host�port�unix_socket_path�db�passwordr � startup_nodes�skip_full_coverage_check)�__opts__�getr r r r r � s ��r c C s^ t rt S | s t� } | d rt| d | d d�a t S tj| d | d | d | d | d d �a t S )zL Return the Redis server instance. Caching the object instance. r r r )r r r r r r r )r r r )�REDIS_SERVERr r r ZStrictRedis)�optsr r r �_get_redis_server� s$ ���r! c C s8 t �dt�t �dt�t �dt�t �dt�t �dt�d�S )z7 Build the key opts based on the user options. zcache.redis.bank_prefixzcache.redis.bank_keys_prefixzcache.redis.key_prefixzcache.redis.separatorzcache.redis.timestamp_prefix)�bank_prefix�bank_keys_prefix� key_prefix� separator�timestamp_prefix)r r �_BANK_PREFIX�_BANK_KEYS_PREFIX�_KEY_PREFIX� _SEPARATOR�_TIMESTAMP_PREFIXr r r r �_get_redis_keys_opts s � ��r, c C � t � }dj|d |d | d�S )z; Return the Redis key for the bank given the name. �{prefix}{separator}{bank}r"