PHPredisでElastiCache(Redis)を操作してみた

Redisのクライアントライブラリには、PHPredisとPredisがあるが、処理速度とPredisはもうメンテナンスが行われていないようなので、C言語の拡張ライブラリであるPHPredisを使ってRedisに対するCRUDアプリを実装してみた。

ライブラリのインストールは、Linux(RHEL/CentOS)の場合、yum install php-pecl-redisだけでOK。

```PHP
<html>
<title>Predis connection test</title>
<body>
  <table border=1>
  <form method="POST" action="">
  <input type="submit" value="登録" style="width:110px;height:40px" name="sub1">
  key: <input type="text" name="key1">
  value: <input type="text" name="value"><br>
  <input type="submit" value="削除" style="width:110px;height:40px" name="sub1">
  key: <input type="text" name="key2"><br>
  <input type="submit" value="全削除" style="width:110px;height:40px" name="sub1"><br>
  <input type="submit" value="全検索" style="width:110px;height:40px" name="sub1"><br>
  <input type="submit" value="セッション登録" style="width:110px;height:40px" name="sub1">
  uid: <input type="text" name="key3"><br>
  </form>
<?php
use Predis\Client;
class RedisCon{
  protected $host;
  protected $port;
  public function __construct(){
    $host = "*****"; #Redisエンドポイント
    $port = 6379;
    $this->redis = new Redis();
    $this->redis->connect($host, $port);
    if (!$this->redis->connect($host, $port)){
      throw new Exception ('failed to connect elasticache redis host.');
    }
  }
  public function SetSession($uid){
    session_start();
    $_SESSION['uid'] = $uid;
    echo "<script type='text/javascript'>alert('Session".$uid." has been registered.');</script>";
  }
  public function SetKey($key, $value){
    if (empty($key)){
      echo "<script type='text/javascript'>alert('Enter some key to delete.');</script>";
      exit;
    }
    $res = $this->redis->exists($key);
      if (!($res)){
        echo "<script type='text/javascript'>alert('". $key. " is NOT found, set key in store.');</script>";
          try{
            $this->redis->set($key, $value);
          }catch(Exception $e){
            echo $e;
          }
      }else{
        echo "<script type='text/javascript'>alert('". $key. " is already exist.');</script>";
      }
  }
  public function DeleteKey($key){
    $res = $this->redis->exists($key);
    if(!($res)){
      echo "<script type='text/javascript'>alert('". $key. " is NOT found. Please enter existing key.');</script>";
      exit;
      }else{
        try{
          $res = $this->redis->del($key);
          echo "<script type='text/javascript'>alert('". $key. " has been deleted.');</script>";
        }catch(Exception $e){
          echo $e;
        }
      }
  }
  public function flashKeys(){
    try{
      $this->redis->flushAll();
      echo "<script type='text/javascript'>alert('All keys have been flushed.');</script>";
    }catch(Exception $e){
      echo $e;
    }
  }
  public function GetAllkeys(){
    $keys = $this->redis->keys('*');
    echo "<th>KEY</th><th>VALUE</th>";
    foreach($keys as $key){
      $value = $this->redis->get($key);
      echo "<tr>";
      echo "<td>".$key."</td>";
      echo "<td>".$value."</td>";
      echo "<tr>";
      }
    }
}
$redis = new RedisCon();
if (isset($_POST["sub1"])){
  $kbn = htmlspecialchars($_POST["sub1"], ENT_QUOTES, "UTF-8");
  switch ($kbn) {
    case "登録": $redis->SetKey($_POST["key1"], $_POST["value"]); break;
    case "削除": $redis->DeleteKey($_POST["key2"]); break;
    case "全削除": $redis->flashKeys(); break;
    case "全検索": $redis->GetAllkeys(); break;
    case "セッション登録": $redis->SetSession($_POST["key3"]); break;
  }
}
?>
</table>
</body>
</html>
```