您好,登錄后才能下訂單哦!
Redis模塊路徑
/usr/lib/python2.6/site-packages/salt/modules/redismod.py
Redis模塊會首先檢查是否安裝有Redis的Python驅(qū)動
# -*- 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驅(qū)動安裝了才返回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驅(qū)動連接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()
異步保存數(shù)據(jù)到磁盤
測試:
$ 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)
設(shè)置某個參數(shù)的值
測試
$ 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的數(shù)量
測試:
$ 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
def expire(key, seconds, host=None, port=None, db=None, password=None): ''' Set a keys time to live in seconds CLI Example: .. code-block:: bash salt '*' redis.expire foo 300 ''' server = _connect(host, port, db, password) return server.expire(key, seconds)
設(shè)置一個KEY的過期時間
測試:
$ sudo salt 'jialebi-qa-server' redis.expire foo 300 127.0.0.1 6379 0 jialebi-qa-server: True
def expireat(key, timestamp, host=None, port=None, db=None, password=None): ''' Set a keys expire at given UNIX time CLI Example: .. code-block:: bash salt '*' redis.expireat foo 1400000000 ''' server = _connect(host, port, db, password) return server.expireat(key, timestamp)
設(shè)置一個KEY在給定的時間過期
$ sudo salt 'jialebi-qa-server' redis.expireat foo 1431532800 127.0.0.1 6379 0 jialebi-qa-server: True
def flushall(host=None, port=None, db=None, password=None): ''' Remove all keys from all databases CLI Example: .. code-block:: bash salt '*' redis.flushall ''' server = _connect(host, port, db, password) return server.flushall()
清空所有數(shù)據(jù)庫
def flushdb(host=None, port=None, db=None, password=None): ''' Remove all keys from the selected database CLI Example: .. code-block:: bash salt '*' redis.flushdb ''' server = _connect(host, port, db, password) return server.flushdb()
清楚選定庫的所有KEY
def get_key(key, host=None, port=None, db=None, password=None): ''' Get redis key value CLI Example: .. code-block:: bash salt '*' redis.get_key foo ''' server = _connect(host, port, db, password) return server.get(key)
獲取一個KEY的值
$ sudo salt 'jialebi-qa-server' redis.get_key foo 127.0.0.1 6379 0 jialebi-qa-server: test
def hget(key, field, host=None, port=None, db=None, password=None): ''' Get specific field value from a redis hash, returns dict CLI Example: .. code-block:: bash salt '*' redis.hget foo_hash bar_field ''' server = _connect(host, port, db, password) return server.hget(key, field)
獲取一個Redis哈希的指定字段的值
測試:
$ sudo salt 'jialebi-qa-server' redis.hget myhash field1 127.0.0.1 6379 0 jialebi-qa-server: foo
def hgetall(key, host=None, port=None, db=None, password=None): ''' Get all fields and values from a redis hash, returns dict CLI Example: .. code-block:: bash salt '*' redis.hgetall foo_hash ''' server = _connect(host, port, db, password) return server.hgetall(key)
獲取一個哈希的所有字段
測試:
$ sudo salt 'jialebi-qa-server' redis.hgetall myhash 127.0.0.1 6379 0 jialebi-qa-server: ---------- field1: foo
def info(host=None, port=None, db=None, password=None): ''' Get information and statistics about the server CLI Example: .. code-block:: bash salt '*' redis.info ''' server = _connect(host, port, db, password) return server.info()
獲取服務(wù)器信息
$ sudo salt 'jialebi-qa-server' redis.info 127.0.0.1 6379 0 jialebi-qa-server: ---------- aof_current_rewrite_time_sec: -1 aof_enabled: 0 aof_last_bgrewrite_status: ok aof_last_rewrite_time_sec: 2 aof_rewrite_in_progress: 0 aof_rewrite_scheduled: 0 arch_bits: 64 blocked_clients: 0 client_biggest_input_buf: 0 client_longest_output_list: 0 connected_clients: 3 connected_slaves: 0 db0: ---------- expires: 1 keys: 3 db1: ---------- expires: 0 keys: 10 db2: ---------- expires: 0 keys: 357 db3: ---------- expires: 0 keys: 12502 db4: ---------- expires: 0 keys: 5173 evicted_keys: 0 expired_keys: 21366 gcc_version: 4.4.7 instantaneous_ops_per_sec: 0 keyspace_hits: 11673041 keyspace_misses: 115559 latest_fork_usec: 21412 loading: 0 lru_clock: 226477 mem_allocator: jemalloc-3.2.0 mem_fragmentation_ratio: 0.46 multiplexing_api: epoll os: Linux 2.6.32-431.1.2.0.1.el6.x86_64 x86_64 process_id: 1481 pubsub_channels: 0 pubsub_patterns: 0 rdb_bgsave_in_progress: 0 rdb_changes_since_last_save: 0 rdb_current_bgsave_time_sec: -1 rdb_last_bgsave_status: ok rdb_last_bgsave_time_sec: 1 rdb_last_save_time: 1428327615 redis_git_dirty: 0 redis_git_sha1: 0 redis_mode: standalone redis_version: 2.6.9 rejected_connections: 0 role: master run_id: 62ec3260542f9bdaa1ead635b1ab4cda55991fd6 tcp_port: 6379 total_commands_processed: 14470609 total_connections_received: 1100003 uptime_in_days: 194 uptime_in_seconds: 16781078 used_cpu_sys: 94220.77 used_cpu_sys_children: 1863.2 used_cpu_user: 32682.53 used_cpu_user_children: 6544.72 used_memory: 65337760 used_memory_human: 62.31M used_memory_lua: 31744 used_memory_peak: 65875152 used_memory_peak_human: 62.82M used_memory_rss: 30359552
def keys(pattern='*', host=None, port=None, db=None, password=None): ''' Get redis keys, supports glob style patterns CLI Example: .. code-block:: bash salt '*' redis.keys salt '*' redis.keys test* ''' server = _connect(host, port, db, password) return server.keys(pattern)
獲取所有的KEY
測試:
$ sudo salt 'jialebi-qa-server' redis.keys '*' 127.0.0.1 6379 0 jialebi-qa-server: - myhash - foo - foot $ sudo salt 'jialebi-qa-server' redis.keys 'foo*' 127.0.0.1 6379 0 jialebi-qa-server: - foo - foot
def key_type(key, host=None, port=None, db=None, password=None): ''' Get redis key type CLI Example: .. code-block:: bash salt '*' redis.type foo ''' server = _connect(host, port, db, password) return server.type(key)
獲取一個KEY的類型
測試:
$ sudo salt 'jialebi-qa-server' redis.key_type foo 127.0.0.1 6379 0 jialebi-qa-server: string $ sudo salt 'jialebi-qa-server' redis.key_type myhash 127.0.0.1 6379 0 jialebi-qa-server: hash
def lastsave(host=None, port=None, db=None, password=None): ''' Get the UNIX time in seconds of the last successful save to disk CLI Example: .. code-block:: bash salt '*' redis.lastsave ''' server = _connect(host, port, db, password) return int(server.lastsave().strftime("%s"))
獲取指定庫上次保存數(shù)據(jù)到磁盤的UNIX時間戳,以秒計算
這里使用strftime()將一個時間元組轉(zhuǎn)換成秒
In [27]: import time,redis In [28]: conn=redis.StrictRedis('127.0.0.1',6379,0) In [29]: conn.lastsave() Out[29]: datetime.datetime(2015, 4, 7, 10, 26, 57) In [30]: conn.lastsave().strftime('%s') Out[30]: '1428373617'
測試:
$ sudo salt 'jialebi-qa-server' redis.lastsave 127.0.0.1 6379 0 jialebi-qa-server: 1428374519
def llen(key, host=None, port=None, db=None, password=None): ''' Get the length of a list in Redis CLI Example: .. code-block:: bash salt '*' redis.llen foo_list ''' server = _connect(host, port, db, password) return server.llen(key)
獲取一個Redis列表的長度
測試:
$ sudo salt 'jialebi-qa-server' redis.llen mylist 127.0.0.1 6379 0 jialebi-qa-server: 3
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。