大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Redis模块路径
成都创新互联,为您提供重庆网站建设公司、网站制作、网站营销推广、网站开发设计,对服务成都墙体彩绘等多个行业拥有丰富的网站建设及推广经验。成都创新互联网站建设公司成立于2013年,提供专业网站制作报价服务,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏心悦目的作品。 与客户共同发展进步,是我们永远的责任!/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: Truedef 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: Truedef 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: 0def 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: Truedef 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: 12502def 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: Truedef 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)设置一个KEY的过期时间
测试:
$ sudo salt 'jialebi-qa-server' redis.expire foo 300 127.0.0.1 6379 0 jialebi-qa-server: Truedef 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)设置一个KEY在给定的时间过期
$ sudo salt 'jialebi-qa-server' redis.expireat foo 1431532800 127.0.0.1 6379 0 jialebi-qa-server: Truedef 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()清空所有数据库
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: testdef 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: foodef 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: foodef 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()获取服务器信息
$ 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: 30359552def 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 - footdef 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: hashdef 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"))获取指定库上次保存数据到磁盘的UNIX时间戳,以秒计算
这里使用strftime()将一个时间元组转换成秒
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: 1428374519def 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另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。