Index.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace app\admin\controller\faredis;
  3. use app\common\controller\Backend;
  4. class Index extends Backend
  5. {
  6. protected $handler = null;
  7. public function _initialize($options = [])
  8. {
  9. parent::_initialize();
  10. if (!extension_loaded('redis')) {
  11. throw new \BadFunctionCallException('未安装redis扩展');
  12. }
  13. $options = get_addon_config('faredis');
  14. $this->handler = new \Redis;
  15. try {
  16. $this->handler->connect($options['host'], $options['port'], 0);
  17. if ('' != $options['password']) {
  18. $this->handler->auth($options['password']);
  19. }
  20. } catch (\RedisException $e) {
  21. $this->error('连接失败,请检查配置文件或Redis服务是否开启', url('faredis/index'), null, 3);
  22. }
  23. }
  24. public function index()
  25. {
  26. if ($this->request->isAjax()) {
  27. $root = array('text' => 'Redis', 'id' => -1, 'type' => 'root', 'state' => array('opened' => true));
  28. $nums = get_addon_config('faredis')['dbnums'];
  29. for ($i = 1; $i <= $nums; $i++) {
  30. $this->handler->select($i - 1);
  31. $count = $this->handler->dbSize();
  32. $root['children'][] = array('text' => 'DB' . ($i - 1) . '(' . $count . ')', 'id' => $i, 'type' => 'db');
  33. }
  34. return json($root);
  35. }
  36. // add test data
  37. // for ($i = 1; $i < 20; $i++) {
  38. // $this->handler->select(0);
  39. // $this->handler->set('k' . $i, $i);
  40. // }
  41. return $this->view->fetch();
  42. }
  43. public function reloadDb($db)
  44. {
  45. $this->handler->select($db - 1);
  46. $node = array('text' => 'DB' . ($db - 1) . '(' . $this->handler->dbSize() . ')', 'id' => $db, 'type' => 'db', 'parent' => -1);
  47. return json($node);
  48. }
  49. public function flushDb($db)
  50. {
  51. $this->handler->select($db - 1);
  52. $this->handler->flushDb();
  53. $this->success();
  54. }
  55. public function keys($db)
  56. {
  57. $this->handler->select($db);
  58. $keys = $this->handler->keys('*');
  59. $data = [];
  60. foreach ($keys as $k => $v) {
  61. $data[] = array('text' => $v, 'id' => $db . '_' . $v, 'type' => 'key');
  62. }
  63. return json($data);
  64. }
  65. public function getValue($db, $key)
  66. {
  67. $this->handler->select($db);
  68. $type = $this->handler->type($key);
  69. $ttl = $this->handler->ttl($key);
  70. $value = null;
  71. // 1 string 2 set 3 list 4 zset 5 hash
  72. switch ($type) {
  73. case 1:
  74. $value = $this->handler->get($key);
  75. break;
  76. case 2:
  77. $value = $this->handler->SMEMBERS($key);
  78. break;
  79. case 3:
  80. $value = $this->handler->lrange($key, 0, -1);
  81. break;
  82. case 4:
  83. $value = $this->handler->zrange($key, 0, -1, true);
  84. break;
  85. case 5:
  86. $value = $this->handler->hGetAll($key);
  87. break;
  88. default:
  89. # code...
  90. break;
  91. }
  92. return json(array('db' => $db, 'key' => $key, 'type' => $type, 'value' => $value, 'ttl' => $ttl));
  93. }
  94. public function delKey($db, $key)
  95. {
  96. $this->handler->select($db);
  97. $r = $this->handler->del($key);
  98. $r == 1 ? $this->success() : $this->error();
  99. }
  100. public function rename($db, $key)
  101. {
  102. $this->handler->select($db);
  103. $newkey = input('newkey', $key);
  104. $r = $this->handler->rename($key, $newkey);
  105. $r == 1 ? $this->success() : $this->error();
  106. }
  107. public function ttl($db, $key)
  108. {
  109. $this->handler->select($db);
  110. $newttl = input('ttl');
  111. if ($newttl == -1)
  112. $this->handler->persist($key);
  113. else
  114. $this->handler->expire($key, $newttl);
  115. $this->success();
  116. }
  117. public function updateValue()
  118. {
  119. $db = input('db');
  120. $key = input('key');
  121. $value = input('value');
  122. $json = json_decode($value);
  123. if (!is_null($json)) {
  124. $value = json_encode($json);
  125. }
  126. $this->handler->select($db);
  127. $type = $this->handler->type($key);
  128. // 1 string 2 set 3 list 4 zset 5 hash
  129. switch ($type) {
  130. case 1:
  131. $this->handler->set($key, $value);
  132. break;
  133. case 2:
  134. $old = input('oldvalue');
  135. $this->handler->sRem($key, $old);
  136. $this->handler->sadd($key, $value);
  137. break;
  138. case 3:
  139. $list_idx = input('oldvalue');
  140. $this->handler->lSet($key, $list_idx, $value);
  141. break;
  142. case 4:
  143. $zset_key = input('zset-key'); //rem
  144. $zset_score = input('zset-score'); //add + value
  145. $this->handler->zrem($key, $zset_key);
  146. $this->handler->zadd($key, $zset_score, $value);
  147. break;
  148. case 5:
  149. $hash_key = input('hash-key');
  150. $this->handler->hSet($key, $hash_key, $value);
  151. break;
  152. default:
  153. # code...
  154. break;
  155. }
  156. $this->success('操作成功');
  157. }
  158. public function addValue()
  159. {
  160. $db = input('db');
  161. $key = input('key');
  162. $value = input('value');
  163. $json = json_decode($value);
  164. if (!is_null($json)) {
  165. $value = json_encode($json);
  166. }
  167. $this->handler->select($db);
  168. $type = $this->handler->type($key);
  169. // 1 string 2 set 3 list 4 zset 5 hash
  170. switch ($type) {
  171. case 2:
  172. $this->handler->sadd($key, $value);
  173. break;
  174. case 3:
  175. $this->handler->rpush($key, $value);
  176. break;
  177. case 4:
  178. $zset_score = input('zset_score', 0);
  179. $this->handler->zadd($key, $zset_score, $value);
  180. break;
  181. case 5:
  182. $hash_key = input('hash_key');
  183. $this->handler->hset($key, $hash_key, $value);
  184. break;
  185. default:
  186. # code...
  187. break;
  188. }
  189. $this->success('操作成功');
  190. }
  191. public function delValue()
  192. {
  193. $db = input('db');
  194. $key = input('key');
  195. $value = input('value');
  196. $json = json_decode($value);
  197. if (!is_null($json)) {
  198. $value = json_encode($json);
  199. }
  200. $this->handler->select($db);
  201. $type = $this->handler->type($key);
  202. // 1 string 2 set 3 list 4 zset 5 hash
  203. switch ($type) {
  204. case 2:
  205. $this->handler->srem($key, $value);
  206. break;
  207. case 3:
  208. $this->handler->lrem($key, $value);
  209. break;
  210. case 4:
  211. $this->handler->zrem($key, $value);
  212. break;
  213. case 5:
  214. $this->handler->hdel($key, $value);
  215. break;
  216. default:
  217. # code...
  218. break;
  219. }
  220. $this->success('操作成功');
  221. }
  222. public function newKey()
  223. {
  224. $db = input('newdb', 1);
  225. --$db;
  226. $key = input('newkey');
  227. $value = input('newvalue');
  228. $json = json_decode($value);
  229. if (!is_null($json)) {
  230. $value = json_encode($json);
  231. }
  232. $this->handler->select($db);
  233. $type = input('newType', 'string');
  234. // 1 string 2 set 3 list 4 zset 5 hash
  235. switch ($type) {
  236. case 'string':
  237. $this->handler->set($key, $value);
  238. break;
  239. case 'set':
  240. $this->handler->sadd($key, $value);
  241. break;
  242. case 'list':
  243. $this->handler->rpush($key, $value);
  244. break;
  245. case 'zset':
  246. $zset_score = input('newscore', 0);
  247. $this->handler->zadd($key, $zset_score, $value);
  248. break;
  249. case 'hash':
  250. $hash_key = input('newfield');
  251. $this->handler->hset($key, $hash_key, $value);
  252. break;
  253. default:
  254. # code...
  255. break;
  256. }
  257. $this->success('操作成功');
  258. }
  259. }