3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Redis の WebAPI を作成

Last updated at Posted at 2020-01-14

Redis の WebAPI をさまざまな言語、フレームワークで作成します。
まずは、仕様です。

サーバーで Redis が動いていること。

key に対して、JSON 文字列を保存するという運用をします。

$ sudo systemctl status redis
● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; preset:>
     Active: active (running) since Mon 2023-05-29 10:45:21 JST; 6h ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 1001 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 37674)
     Memory: 6.8M
        CPU: 20.807s
     CGroup: /system.slice/redis-server.service
             └─1001 "/usr/bin/redis-server 127.0.0.1:6379"

作成する API は次の3つです。method は POST です。

key を与えて値の読み出し。
key と value (JSON 文字列)を与えて書き込み。
key の一覧

テストスクリプト

サーバーサイドに次のプログラムがあるとします。

http://localhost/test_dir/api/redis_read.py
http://localhost/test_dir/api/redis_insert.py
http://localhost/test_dir/api/redis_list.py

プログラムはこちら
Redis の WebAPI (python CGI)

curl によるテストスクリプト

test_get.sh
#
URL="http://localhost/test_dir/api/redis_read.py"
#
curl -X POST -d key=t1855 $URL
#
test_insert.sh
#
URL="http://localhost/test_dir/api/redis_insert.py"
#
curl -X POST -d key=t1855 \
-d value='{"name": "宇都宮","population": 87516,"date_mod": "2001-3-16"}' $URL
#
test_list.sh
#
URL="http://localhost/test_dir/api/redis_list.py"
#
curl -X POST $URL
#

Httpie によるテストスクリプト

httpie_get.sh
URL="http://localhost/test_dir/api/redis_read.py"
#
http --form POST $URL key=t1856
httpie_insert.sh
URL="http://localhost/test_dir/api/redis_insert.py"
#
http --form POST $URL key=t1856 value='{"name": "小山","population": 73125,"date_mod": "2001-6-8"}'
httpie_list.sh
URL="http://localhost/test_dir/api/redis_list.py"
#
http POST $URL

python によるテストプログラム

test_get.py
#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
#	test_get.py
#
#					Jan/14/2020
#
# ------------------------------------------------------------------
import  sys
import  requests
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
url="http://localhost/test_dir/api/redis_read.py"
args={}
args['key'] = 't1855'
#
rr=requests.post(url,args)
print(rr.text)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
test_insert.py
#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
#	test_insert.py
#
#					Jan/14/2020
#
# ------------------------------------------------------------------
import  sys
import  requests
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
url="http://localhost/test_dir/api/redis_insert.py"
args={}
args['key'] = 't1855'
args['value'] = '{"name": "宇都宮","population": 84516,"date_mod": "2001-3-16"}'
#
rr=requests.post(url,args)
print(rr.text)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
test_list.py
#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
#	test_list.py
#
#					Jan/14/2020
#
# ------------------------------------------------------------------
import  sys
import  requests
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
url="http://localhost/test_dir/api/redis_list.py"
args={}
#
rr=requests.post(url,args)
print(rr.text)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

サーバーサイド

Redis の WebAPI (python CGI)
Redis の WebAPI (PHP CGI)
Redis の WebAPI (Ruby CGI)
Redis の WebAPI (Express)
Redis の WebAPI (oak)
Redis の WebAPI (Nuxt.js)
Redis の WebAPI (Gin)
Redis の WebAPI (FastAPI)
Redis の WebAPI (Genie)

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?