Redis模块路径
/usr/lib/python2.6/site-packages/salt/modules/redismod.py
Redis模块会首先检查是否安装有Redis的Python驱动
# -*- coding: utf-8 -*- ‘‘‘ Module to provide redis functionality to Salt .. versionadded:: 2014.7.0 :configuration: This module requires the redis python module and uses the following defaults which may be overridden in the minion configuration: .. code-block:: yaml redis.host: ‘localhost‘ redis.port: 6379 redis.db: 0 redis.password: None ‘‘‘ # Import third party libs try: import redis HAS_REDIS = True except ImportError: HAS_REDIS = False __virtualname__ = ‘redis‘
def __virtual__(): ‘‘‘ Only load this module if redis python module is installed ‘‘‘ if HAS_REDIS: return __virtualname__ else: return False
只有当Redis的Python驱动安装了才返回SaltStack的Redis模块名称redis
def _connect(host=None, port=None, db=None, password=None): ‘‘‘ Returns an instance of the redis client ‘‘‘ if not host: host = __salt__[‘config.option‘](‘redis.host‘) if not port: port = __salt__[‘config.option‘](‘redis.port‘) if not db: db = __salt__[‘config.option‘](‘redis.db‘) if not password: password = __salt__[‘config.option‘](‘redis.password‘) return redis.StrictRedis(host, port, db, password)
如果没有指定Redis实例的IP,端口和DB名称以及密码就去读取配置文件。这里使用redis的Python驱动连接Redis
def bgrewriteaof(host=None, port=None, db=None, password=None): ‘‘‘ Asynchronously rewrite the append-only file CLI Example: .. code-block:: bash salt ‘*‘ redis.bgrewriteaof ‘‘‘ server = _connect(host, port, db, password) return server.bgrewriteaof()
异步重写AOF文件
测试
$ sudo salt ‘jialebi-qa-server‘ redis.bgrewriteaof 127.0.0.1 6379 0 jialebi-qa-server: True
def bgsave(host=None, port=None, db=None, password=None): ‘‘‘ Asynchronously save the dataset to disk CLI Example: .. code-block:: bash salt ‘*‘ redis.bgsave ‘‘‘ server = _connect(host, port, db, password) return server.bgsave()
异步保存数据到磁盘
测试:
$ sudo salt ‘jialebi-qa-server‘ redis.bgsave 127.0.0.1 6379 0 jialebi-qa-server: True
def config_get(pattern=‘*‘, host=None, port=None, db=None, password=None): ‘‘‘ Get redis server configuration values CLI Example: .. code-block:: bash salt ‘*‘ redis.config_get salt ‘*‘ redis.config_get port ‘‘‘ server = _connect(host, port, db, password) return server.config_get(pattern)
获取配置信息
测试:
$ sudo salt ‘jialebi-qa-server‘ redis.config_get ‘*‘ 127.0.0.1 6379 0 jialebi-qa-server: ---------- activerehashing: yes appendfsync: everysec appendonly: no auto-aof-rewrite-min-size: 67108864 auto-aof-rewrite-percentage: 100 bind: 127.0.0.1 client-output-buffer-limit: normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60 daemonize: yes databases: 16 dbfilename: dump.rdb dir: /data/app_data/redis/data hash-max-ziplist-entries: 512 hash-max-ziplist-value: 64 list-max-ziplist-entries: 512 list-max-ziplist-value: 64 logfile: /data/app_data/redis/logs/redis.log loglevel: notice lua-time-limit: 5000 masterauth: maxclients: 10000 maxmemory: 0 maxmemory-policy: volatile-lru maxmemory-samples: 3 no-appendfsync-on-rewrite: no pidfile: /var/run/redis.pid port: 6379 rdbchecksum: yes rdbcompression: yes repl-ping-slave-period: 10 repl-timeout: 60 requirepass: save: 900 1 300 10 60 10000 set-max-intset-entries: 512 slave-priority: 100 slave-read-only: yes slave-serve-stale-data: yes slaveof: slowlog-log-slower-than: 10000 slowlog-max-len: 128 stop-writes-on-bgsave-error: yes timeout: 0 unixsocket: unixsocketperm: 0 watchdog-period: 0 zset-max-ziplist-entries: 128 zset-max-ziplist-value: 64
$ sudo salt ‘jialebi-qa-server‘ redis.config_get ‘maxmemory‘ 127.0.0.1 6379 0 jialebi-qa-server: ---------- maxmemory: 0
def config_set(name, value, host=None, port=None, db=None, password=None): ‘‘‘ Set redis server configuration values CLI Example: .. code-block:: bash salt ‘*‘ redis.config_set masterauth luv_kittens ‘‘‘ server = _connect(host, port, db, password) return server.config_set(name, value)
设置某个参数的值
测试
$ sudo salt ‘jialebi-qa-server‘ redis.config_set ‘maxmemory‘ 300000000 127.0.0.1 6379 0 jialebi-qa-server: True
def dbsize(host=None, port=None, db=None, password=None): ‘‘‘ Return the number of keys in the selected database CLI Example: .. code-block:: bash salt ‘*‘ redis.dbsize ‘‘‘ server = _connect(host, port, db, password) return server.dbsize()
返回选择库的key的数量
测试:
$ sudo salt ‘jialebi-qa-server‘ redis.dbsize 127.0.0.1 6379 3 jialebi-qa-server: 12502
def delete(*keys, **connection_args): ‘‘‘ Deletes the keys from redis, returns number of keys deleted CLI Example: .. code-block:: bash salt ‘*‘ redis.delete foo ‘‘‘ # Get connection args from keywords if set conn_args = {} for arg in [‘host‘, ‘port‘, ‘db‘, ‘password‘]: if arg in connection_args: conn_args[arg] = connection_args[arg] server = _connect(**conn_args) return server.delete(*keys)
删除一个KEY
def exists(key, host=None, port=None, db=None, password=None): ‘‘‘ Return true if the key exists in redis CLI Example: .. code-block:: bash salt ‘*‘ redis.exists foo ‘‘‘ server = _connect(host, port, db, password) return server.exists(key)
检查一个KEY是否存在
测试
$ sudo salt ‘jialebi-qa-server‘ redis.exists foo 127.0.0.1 6379 0 jialebi-qa-server: True
本文出自 “Linux SA John” 博客,请务必保留此出处http://john88wang.blog.51cto.com/2165294/1628981
原文地址:http://john88wang.blog.51cto.com/2165294/1628981