redis模塊路徑
創(chuàng)新互聯(lián)公司秉承實(shí)現(xiàn)全網(wǎng)價(jià)值營銷的理念,以專業(yè)定制企業(yè)官網(wǎng),成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作,微信小程序開發(fā),網(wǎng)頁設(shè)計(jì)制作,成都做手機(jī)網(wǎng)站,成都營銷網(wǎng)站建設(shè)幫助傳統(tǒng)企業(yè)實(shí)現(xiàn)“互聯(lián)網(wǎng)+”轉(zhuǎn)型升級專業(yè)定制企業(yè)官網(wǎng),公司注重人才、技術(shù)和管理,匯聚了一批優(yōu)秀的互聯(lián)網(wǎng)技術(shù)人才,對客戶都以感恩的心態(tài)奉獻(xiàn)自己的專業(yè)和所長。
/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
只有當(dāng)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實(shí)例的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è)置某個(gè)參數(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)
刪除一個(gè)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)
檢查一個(gè)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è)置一個(gè)KEY的過期時(shí)間
測試:
$ 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è)置一個(gè)KEY在給定的時(shí)間過期
$ 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)
獲取一個(gè)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)
獲取一個(gè)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)
獲取一個(gè)哈希的所有字段
測試:
$ 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()
獲取 獲取所有的KEY 測試: 獲取一個(gè)KEY的類型 測試: 獲取指定庫上次保存數(shù)據(jù)到磁盤的UNIX時(shí)間戳,以秒計(jì)算 這里使用strftime()將一個(gè)時(shí)間元組轉(zhuǎn)換成秒 測試: 獲取一個(gè)Redis列表的長度 測試: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)
$ 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)
$ 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"))
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)
$ sudo salt 'jialebi-qa-server' redis.llen mylist 127.0.0.1 6379 0
jialebi-qa-server:
3
文章名稱:SaltStack源碼分析之使用Redis模塊
本文URL:http://weahome.cn/article/jioosc.html