ElastiCache Redis を Amazon Linux で使う

AWS の ElasticCache Redis を Amazon Linux で使う方法です。

環境の準備　python3 と ライブラリーのインストール

```bash
sudo yum install python36 python36-devel python36-libs
sudo pip-3.6 install redis
```

Create

```py:redis_create.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
#	redis_create.py
#
#					Apr/24/2018
#
import sys
import json
import redis
#
# ---------------------------------------------------------------
def     dict_append_proc(dict_aa,key,name,population,date_mod):
	dict_aa[key] = {'name':name,'population':population,'date_mod':date_mod}
#
	return dict_aa
#
# ----------------------------------------------------------------
def	data_prepare_proc():
	dict_aa = {} 
#
	dict_aa = dict_append_proc(dict_aa,'t1851','福井',52761,'2003-4-30')
	dict_aa = dict_append_proc(dict_aa,'t1852','敦賀',41295,'2003-5-10')
	dict_aa = dict_append_proc(dict_aa,'t1853','小浜',28674,'2003-6-14')
	dict_aa = dict_append_proc(dict_aa,'t1854','大野',89612,'2003-9-9')
	dict_aa = dict_append_proc(dict_aa,'t1855','勝山',47391,'2003-8-4')
	dict_aa = dict_append_proc(dict_aa,'t1856','鯖江',35187,'2003-1-21')
	dict_aa = dict_append_proc(dict_aa,'t1857','あわら',81246,'2003-7-23')
	dict_aa = dict_append_proc(dict_aa,'t1858','越前',25791,'2003-10-26')
	dict_aa = dict_append_proc(dict_aa,'t1859','坂井',84139,'2003-12-15')
#
	return	dict_aa
#
# ----------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
rr = redis.Redis(host='aaa-001.07abcd.0001.apne1.cache.amazonaws.com', port=6379, db=0)
#
dict_aa = data_prepare_proc()
#
for key in dict_aa.keys():
	print(key)
	value = dict_aa[key]
	json_str = json.dumps(value)
	rr.set(key, json_str)
#
sys.stderr.write("*** 終了 ***\n")
# ----------------------------------------------------------------
```

Read

```py:redis_read.py
#! /usr/bin/python3
# -*- coding:utf-8 -*-
#
#	redis_read.py
#
#					Apr/24/2018
#
# --------------------------------------------------------------
import sys
import redis
import json
#
# --------------------------------------------------------------
def dict_display_proc(dict_aa):
	for key in sorted(dict_aa.keys()):
		unit = dict_aa[key]
		name = unit['name']
		str_out = str(key) +"\t"+ str(name)
		str_out += "\t" + str(unit['population'])
		str_out += "\t" + str(unit['date_mod'])
		print(str_out)
# ---------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")

rr = redis.Redis(host='aaa-001.07abcd.0001.apne1.cache.amazonaws.com', port=6379, db=0)

keys = rr.keys('t*')

for key in sorted(keys):
	str_json = rr.get(key).decode ()
	unit_aa = json.loads(str_json)
	str_out = key.decode()+"\t"+ unit_aa['name']
	str_out += "\t"+ str(unit_aa['population'])
	str_out += "\t"+ unit_aa['date_mod']
	print(str_out)
#
sys.stderr.write("*** 終了 ***\n")
#
# --------------------------------------------------------------
```

Update

```py:redis_update.py
#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
#	redis_update.py  t1852 99999
#
#					Apr/24/2018
#
import	sys
import	string
import	json
#
import	datetime
import	redis
#
# ------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
key_in = sys.argv[1]
population_in = int(sys.argv[2])
print("%s\t%d" % (key_in, population_in))
#
rr = redis.Redis(host='aaa-001.07abcd.0001.apne1.cache.amazonaws.com', port=6379, db=0)
#
str_json = rr.get(key_in).decode ()
unit_aa = json.loads(str_json)
now = datetime.datetime.now()
unit_aa['population'] = population_in
unit_aa['date_mod'] =  '%s' % now
json_str = json.dumps(unit_aa)
rr.set(key_in, json_str)
#
sys.stderr.write("*** 終了 ***\n")
#
# ------------------------------------------------------------
```

Delete

```py:redis_delete.py
#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
#	redis_delete.py  t1854
#
#					Apr/24/2018
#
import	sys
import	redis
#
# ------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
key_in = sys.argv[1]
print ("%s" % key_in)
#
rr = redis.Redis(host='aaa-001.07abcd.0001.apne1.cache.amazonaws.com', port=6379, db=0)
#
str_key = str(key_in)
rr.delete(str_key)
#
sys.stderr.write("*** 終了 ***\n")
#
# ------------------------------------------------------------
```