14 May 2016

setup

  1. create a container with docker

    1. pull redis image

       $ docker pull redis
      
    2. start a redis instance

       $ docker run -p 6379:6379 --name gredis -d redis
      
  2. redis instance info

    1. check

       $ docker ps -a
       CONTAINER ID IMAGE COMMAND                CREATED        STATUS       PORTS                  NAMES
       2cb741aea9e2 redis "docker-entrypoint.sh" 28 minutes ago Up 8 minutes 0.0.0.0:6379->6379/tcp gredis
      
    2. ip

       $ docker inspect --format='\{\{\.NetworkSettings\.IPAddress\}\}' gredis
       172.17.0.2
      
  3. docker redis-cli

    1. run another container

       $ docker run --rm -it  redis redis-cli -h 172.17.0.2
       172.17.0.2:6379> info
       # Server
       redis_version:3.2.4
       redis_git_sha1:00000000
       redis_git_dirty:0
       redis_build_id:53859a8c1aa90905
       redis_mode:standalone
       os:Linux 4.9.13-moby x86_64
       arch_bits:64
       multiplexing_api:epoll
       gcc_version:4.9.2
       process_id:1
       run_id:2aea96a321bf5082d66f458bb8202ac098583c88
       tcp_port:6379
       uptime_in_seconds:429
       uptime_in_days:0
       hz:10
       lru_clock:14996259
       executable:/data/redis-server
       ...
      
    2. command get set demo

       172.17.0.2:6379> set helloworld 2
       OK
       172.17.0.2:6379> get helloworld
       "2"
       172.17.0.2:6379> set helloworld "hello world"
       OK
       172.17.0.2:6379> get helloworld
       "hello world"
      
    3. all commands

  4. python code

    1. zset.poc.py on my github

       #! coding: utf-8
      
       import redis
       import time
       from datetime import datetime, timedelta
      
       DAYS = 10
       # $ docker run -p 6379:6379 --name gredis -d redis
       conn = redis.Redis(host='localhost', port=6379, db=1)
      
      
       def redis_info():
           """redis_info"""
           info = conn.info()
           for key in info:
               print "%s: %s" % (key, info[key])
      
           print 'dbsize: %s' % conn.dbsize()
           print "ping %s" % conn.ping()
      
      
       def date_format(date):
           """date_format"""
           return date.strftime("%Y%m%d")
      
      
       def zset_poc():
           """zset_poc"""
           # now = datetime.now() - timedelta(days=DAYS)
           now = datetime.strptime('20160514112233', '%Y%m%d%H%M%S') - timedelta(days=DAYS)
           days = [now - timedelta(days=-day) for day in range(DAYS)]
           times = [time.mktime(day.timetuple()) for day in days]
           datas = [(date_format(key), value) for key, value in zip(days, times)]
      
           zset_key = 'day_ts'
           # remove old test if any
           cnt = conn.zcard(zset_key)
           if cnt > 0:
               print 'remove', cnt, 'in zset'
               conn.zremrangebyrank(zset_key, 0, cnt)
           # field 5 to 9 or -5 to -1
           (key_start, ts_start) = datas[-5]
           (key_stop, ts_stop) = datas[-1]
      
           # 0 20160504 1462370790.0
           # 1 20160505 1462457190.0
           # 2 20160506 1462543590.0
           # 3 20160507 1462629990.0
           # 4 20160508 1462716390.0
           # 5 20160509 1462802790.0 <=
           # 6 20160510 1462889190.0
           # 7 20160511 1462975590.0
           # 8 20160512 1463061990.0
           # 9 20160513 1463148390.0 <=
           for i, (key, value) in enumerate(datas):
               print i, key, value,
               if key == key_start:
                   print '<=',
               if key == key_stop:
                   print '<=',
               print
               conn.zadd(zset_key, key, value)
           # ('20160513', 1463148092.0) ('20160509', 1462802492.0)
           print datas[-5], datas[-1]
           # ['20160509', '20160510', '20160511', '20160512', '20160513']
           print conn.zrange(zset_key, 5, 9)
           print conn.zrange(zset_key, -5, -1)
           print conn.zrangebyscore(zset_key, ts_start, ts_stop)
      
       if __name__ == '__main__':
           zset_poc()
      
  5. cleanup

    1. stop

       $ docker stop gredis
      
    2. remove

       $ docker rm gredis
      


blog comments powered by Disqus