「Redis入門」List型を操作するメモ
サンプルコード
$redis = new Redis();
//接続
$redis->connect('127.0.0.1’, 6379);
//リストにデータを保存
$redis->lpush('list’, 'html’);
$redis->lpush('list’, 'html’);
$redis->lpush('list’, 'html’);
$redis->lpush('list’, 'css’);
$redis->lpush('list’, 'php’);
$redis->lpush('list’, 'mysql’);
$redis->lpush('list’, 'javascript’);
$redis->lpush('list’, 'html’);
$redis->lpush('list’, 'html’);
$redis->lpush('list’, 'html’);
$redis->lpush('list’, 'ajax’);
//リストの値を取得
$list = $redis->lrange('list’, 0, -1);
print_r($list);echo '<br>’;
//左->右 要素削除
$redis->lrem('list’, 'html’, 2);
$list = $redis->lrange('list’, 0, -1);
print_r($list);echo '<br>’;
//右->左 要素削除
$redis->lrem('list’, 'html’, -2);
$list = $redis->lrange('list’, 0, -1);
print_r($list);echo '<br>’;
//全部削除
$redis->lrem('list’, 'html’, 0);
$list = $redis->lrange('list’, 0, -1);
print_r($list);echo '<br>’;
実行結果
Array ( [0] => ajax [1] => html [2] => html [3] => html [4] => javascript [5] => mysql [6] => php [7] => css [8] => html [9] => html [10] => html )
Array ( [0] => ajax [1] => html [2] => javascript [3] => mysql [4] => php [5] => css [6] => html [7] => html [8] => html )
Array ( [0] => ajax [1] => html [2] => javascript [3] => mysql [4] => php [5] => css [6] => html )
Array ( [0] => ajax [1] => javascript [2] => mysql [3] => php [4] => css )