├── .gitignore
├── LICENSE
├── README.md
├── composer.json
├── resources
└── views
│ ├── console.blade.php
│ ├── create
│ ├── hash.blade.php
│ ├── list.blade.php
│ ├── nil.blade.php
│ ├── set.blade.php
│ ├── string.blade.php
│ └── zset.blade.php
│ ├── edit
│ ├── hash.blade.php
│ ├── list.blade.php
│ ├── nil.blade.php
│ ├── set.blade.php
│ ├── string.blade.php
│ └── zset.blade.php
│ ├── index.blade.php
│ └── layout.blade.php
└── src
├── BootExtension.php
├── DataType
├── DataType.php
├── Hashes.php
├── Lists.php
├── Sets.php
├── SortedSets.php
└── Strings.php
├── RedisController.php
├── RedisManager.php
└── RedisManagerServiceProvider.php
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | phpunit.phar
3 | /vendor
4 | composer.phar
5 | composer.lock
6 | *.project
7 | .idea/
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Jens Segers
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of
6 | this software and associated documentation files (the "Software"), to deal in
7 | the Software without restriction, including without limitation the rights to
8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software is furnished to do so,
10 | subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Redis manager for laravel-admin
2 | ========================
3 |
4 | [](https://styleci.io/repos/129332701)
5 | [](https://packagist.org/packages/laravel-admin-ext/redis-manager)
6 | [](https://packagist.org/packages/laravel-admin-ext/redis-manager)
7 | []()
8 |
9 | ## Screenshot
10 |
11 | 
12 |
13 | ## Installation
14 |
15 | ```
16 | $ composer require laravel-admin-ext/redis-manager
17 |
18 | $ php artisan admin:import redis-manager
19 | ```
20 |
21 | Open `http://your-host/admin/redis` in your browser.
22 |
23 | License
24 | ------------
25 | Licensed under [The MIT License (MIT)](LICENSE).
26 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "laravel-admin-ext/redis-manager",
3 | "description": "Redis manager for laravel-admin",
4 | "type": "library",
5 | "keywords": ["laravel-admin", "redis", "manager"],
6 | "homepage": "https://github.com/laravel-admin-extensions/redis-manager",
7 | "license": "MIT",
8 | "authors": [
9 | {
10 | "name": "z-song",
11 | "email": "zosong@126.com"
12 | }
13 | ],
14 | "require": {
15 | "php": ">=7.0.0",
16 | "encore/laravel-admin": "~1.5",
17 | "predis/predis": "~1.0"
18 | },
19 | "require-dev": {
20 | "phpunit/phpunit": "~6.0",
21 | "laravel/laravel": "~5.5"
22 | },
23 | "autoload": {
24 | "psr-4": {
25 | "Encore\\Admin\\RedisManager\\": "src/"
26 | }
27 | },
28 | "extra": {
29 | "laravel": {
30 | "providers": [
31 | "Encore\\Admin\\RedisManager\\RedisManagerServiceProvider"
32 | ]
33 |
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/resources/views/console.blade.php:
--------------------------------------------------------------------------------
1 | @extends('laravel-admin-redis-manager::layout')
2 |
3 | @section('page')
4 |
5 |
6 |
7 |
117 |
118 |
119 |
122 |
123 |
124 |
125 |
139 |
140 |
141 |
142 |
143 | @endsection
--------------------------------------------------------------------------------
/resources/views/create/hash.blade.php:
--------------------------------------------------------------------------------
1 | @extends('laravel-admin-redis-manager::layout')
2 |
3 | @section('page')
4 |
5 |
6 |
9 |
10 |
11 |
58 |
59 |
60 |
61 |
62 | @endsection
--------------------------------------------------------------------------------
/resources/views/create/list.blade.php:
--------------------------------------------------------------------------------
1 | @extends('laravel-admin-redis-manager::layout')
2 |
3 | @section('page')
4 |
5 |
6 |
9 |
10 |
11 |
50 |
51 |
52 |
53 |
54 | @endsection
--------------------------------------------------------------------------------
/resources/views/create/nil.blade.php:
--------------------------------------------------------------------------------
1 | @extends('laravel-admin-redis-manager::layout')
2 |
3 | @section('page')
4 |
5 |
6 |
7 |
Key [{{ request('key') }}] not exists.
8 |
9 |
10 |
11 |
12 | @endsection
--------------------------------------------------------------------------------
/resources/views/create/set.blade.php:
--------------------------------------------------------------------------------
1 | @extends('laravel-admin-redis-manager::layout')
2 |
3 | @section('page')
4 |
5 |
16 |
17 |
18 |
21 |
22 |
23 |
65 |
66 |
67 |
68 |
69 | @endsection
--------------------------------------------------------------------------------
/resources/views/create/string.blade.php:
--------------------------------------------------------------------------------
1 | @extends('laravel-admin-redis-manager::layout')
2 |
3 | @section('page')
4 |
5 |
48 |
49 |
50 | @endsection
--------------------------------------------------------------------------------
/resources/views/create/zset.blade.php:
--------------------------------------------------------------------------------
1 | @extends('laravel-admin-redis-manager::layout')
2 |
3 | @section('page')
4 |
5 |
6 |
9 |
10 |
11 |
58 |
59 |
60 |
61 |
62 | @endsection
--------------------------------------------------------------------------------
/resources/views/edit/hash.blade.php:
--------------------------------------------------------------------------------
1 | @extends('laravel-admin-redis-manager::layout')
2 |
3 | @section('page')
4 |
5 |
6 |
9 |
10 |
11 |
102 |
103 |
104 |
105 |
106 |
203 |
204 | @endsection
205 |
--------------------------------------------------------------------------------
/resources/views/edit/list.blade.php:
--------------------------------------------------------------------------------
1 | @extends('laravel-admin-redis-manager::layout')
2 |
3 | @section('page')
4 |
5 |
6 |
9 |
10 |
11 |
97 |
98 |
99 |
100 |
101 |
199 |
200 | @endsection
201 |
--------------------------------------------------------------------------------
/resources/views/edit/nil.blade.php:
--------------------------------------------------------------------------------
1 | @extends('laravel-admin-redis-manager::layout')
2 |
3 | @section('page')
4 |
5 |
6 |
7 |
Key [{{ request('key') }}] not exists.
8 |
9 |
10 |
11 |
12 | @endsection
--------------------------------------------------------------------------------
/resources/views/edit/set.blade.php:
--------------------------------------------------------------------------------
1 | @extends('laravel-admin-redis-manager::layout')
2 |
3 | @section('page')
4 |
5 |
6 |
9 |
10 |
11 |
57 |
58 |
59 |
60 |
61 |
152 |
153 | @endsection
154 |
--------------------------------------------------------------------------------
/resources/views/edit/string.blade.php:
--------------------------------------------------------------------------------
1 | @extends('laravel-admin-redis-manager::layout')
2 |
3 | @section('page')
4 |
5 |
6 |
9 |
10 |
50 |
51 |
52 |
53 |
54 | @endsection
55 |
--------------------------------------------------------------------------------
/resources/views/edit/zset.blade.php:
--------------------------------------------------------------------------------
1 | @extends('laravel-admin-redis-manager::layout')
2 |
3 | @section('page')
4 |
5 |
6 |
9 |
10 |
11 |
102 |
103 |
104 |
105 |
106 |
203 |
204 | @endsection
205 |
--------------------------------------------------------------------------------
/resources/views/index.blade.php:
--------------------------------------------------------------------------------
1 | @extends('laravel-admin-redis-manager::layout')
2 |
3 | @section('page')
4 |
5 |
6 |
14 |
15 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
91 |
92 | @if (empty($keys))
93 |
94 | Empty list or set.
95 |
96 | @endif
97 |
98 |
99 |
100 |
101 |
102 |
198 |
199 | @endsection
--------------------------------------------------------------------------------
/resources/views/layout.blade.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
28 |
29 |
30 |
39 |
40 |
41 |
42 |
43 |
44 | @foreach($connections[$conn] as $name => $value)
45 |
46 | {{ $name }} |
47 | {{ is_array($value) ? json_encode($value) : $value }} |
48 |
49 | @endforeach
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
61 |
62 |
63 |
64 |
65 |
66 | @foreach($info as $part => $detail)
67 |
68 |
75 |
76 |
77 |
78 |
79 | @foreach($detail as $key => $value)
80 |
81 | {{ $key }} |
82 |
83 | @if(is_array($value))
84 | {{ json_encode($value, JSON_PRETTY_PRINT) }}
85 | @else
86 | {{ $value }}
87 | @endif
88 | |
89 |
90 | @endforeach
91 |
92 |
93 |
94 |
95 |
96 | @endforeach
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 | @yield('page')
109 |
110 |
111 |
112 |
113 |
114 |
--------------------------------------------------------------------------------
/src/BootExtension.php:
--------------------------------------------------------------------------------
1 | get('redis', 'Encore\Admin\RedisManager\RedisController@index')->name('redis-index');
26 | $router->delete('redis/key', 'Encore\Admin\RedisManager\RedisController@destroy')->name('redis-key-delete');
27 | $router->get('redis/fetch', 'Encore\Admin\RedisManager\RedisController@fetch')->name('redis-fetch-key');
28 | $router->get('redis/create', 'Encore\Admin\RedisManager\RedisController@create')->name('redis-create-key');
29 | $router->post('redis/store', 'Encore\Admin\RedisManager\RedisController@store')->name('redis-store-key');
30 | $router->get('redis/edit', 'Encore\Admin\RedisManager\RedisController@edit')->name('redis-edit-key');
31 | $router->put('redis/key', 'Encore\Admin\RedisManager\RedisController@update')->name('redis-update-key');
32 | $router->delete('redis/item', 'Encore\Admin\RedisManager\RedisController@remove')->name('redis-remove-item');
33 |
34 | $router->get('redis/console', 'Encore\Admin\RedisManager\RedisController@console')->name('redis-console');
35 | $router->post('redis/console', 'Encore\Admin\RedisManager\RedisController@execute')->name('redis-execute');
36 | });
37 | }
38 |
39 | /**
40 | * {@inheritdoc}
41 | */
42 | public static function import()
43 | {
44 | parent::createMenu('Redis manager', 'redis', 'fa-database');
45 |
46 | parent::createPermission('Redis Manager', 'ext.redis-manager', 'redis*');
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/DataType/DataType.php:
--------------------------------------------------------------------------------
1 | connection = $connection;
22 | }
23 |
24 | /**
25 | * Get redis connection.
26 | *
27 | * @return Connection
28 | */
29 | public function getConnection()
30 | {
31 | return $this->connection;
32 | }
33 |
34 | /**
35 | * @param string $key
36 | *
37 | * @return mixed
38 | */
39 | abstract public function fetch(string $key);
40 |
41 | /**
42 | * @param array $params
43 | *
44 | * @return mixed
45 | */
46 | abstract public function update(array $params);
47 |
48 | /**
49 | * @param array $params
50 | *
51 | * @return mixed
52 | */
53 | abstract public function store(array $params);
54 |
55 | /**
56 | * Returns the remaining time to live of a key that has a timeout.
57 | *
58 | * @param string $key
59 | *
60 | * @return int
61 | */
62 | public function ttl($key)
63 | {
64 | return $this->getConnection()->ttl($key);
65 | }
66 |
67 | /**
68 | * Set a timeout on key.
69 | *
70 | * @param string $key
71 | * @param int $expire
72 | *
73 | * @return void
74 | */
75 | public function setTtl($key, $expire)
76 | {
77 | if (is_null($expire)) {
78 | return;
79 | }
80 |
81 | $expire = (int) $expire;
82 |
83 | if ($expire > 0) {
84 | $this->getConnection()->expire($key, $expire);
85 | } else {
86 | $this->getConnection()->persist($key);
87 | }
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/src/DataType/Hashes.php:
--------------------------------------------------------------------------------
1 | getConnection()->hgetall($key);
13 | }
14 |
15 | /**
16 | * {@inheritdoc}
17 | */
18 | public function update(array $params)
19 | {
20 | $key = array_get($params, 'key');
21 |
22 | if (array_has($params, 'field')) {
23 | $field = array_get($params, 'field');
24 | $value = array_get($params, 'value');
25 |
26 | $this->getConnection()->hset($key, $field, $value);
27 | }
28 |
29 | if (array_has($params, '_editable')) {
30 | $value = array_get($params, 'value');
31 | $field = array_get($params, 'pk');
32 |
33 | $this->getConnection()->hset($key, $field, $value);
34 | }
35 | }
36 |
37 | /**
38 | * {@inheritdoc}
39 | */
40 | public function store(array $params)
41 | {
42 | $key = array_get($params, 'key');
43 | $ttl = array_get($params, 'ttl');
44 | $field = array_get($params, 'field');
45 | $value = array_get($params, 'value');
46 |
47 | $this->getConnection()->hset($key, $field, $value);
48 |
49 | if ($ttl > 0) {
50 | $this->getConnection()->expire($key, $ttl);
51 | }
52 |
53 | return redirect(route('redis-edit-key', [
54 | 'conn' => request('conn'),
55 | 'key' => $key,
56 | ]));
57 | }
58 |
59 | /**
60 | * Remove a field from a hash.
61 | *
62 | * @param array $params
63 | *
64 | * @return int
65 | */
66 | public function remove(array $params)
67 | {
68 | $key = array_get($params, 'key');
69 | $field = array_get($params, 'field');
70 |
71 | return $this->getConnection()->hdel($key, [$field]);
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/DataType/Lists.php:
--------------------------------------------------------------------------------
1 | getConnection()->lrange($key, 0, -1);
13 | }
14 |
15 | /**
16 | * {@inheritdoc}
17 | */
18 | public function update(array $params)
19 | {
20 | $key = array_get($params, 'key');
21 |
22 | if (array_has($params, 'push')) {
23 | $item = array_get($params, 'item');
24 | $command = $params['push'] == 'left' ? 'lpush' : 'rpush';
25 |
26 | $this->getConnection()->{$command}($key, $item);
27 | }
28 |
29 | if (array_has($params, '_editable')) {
30 | $value = array_get($params, 'value');
31 | $index = array_get($params, 'pk');
32 |
33 | $this->getConnection()->lset($key, $index, $value);
34 | }
35 | }
36 |
37 | /**
38 | * {@inheritdoc}
39 | */
40 | public function store(array $params)
41 | {
42 | $key = array_get($params, 'key');
43 | $item = array_get($params, 'item');
44 | $ttl = array_get($params, 'ttl');
45 |
46 | $this->getConnection()->rpush($key, [$item]);
47 |
48 | if ($ttl > 0) {
49 | $this->getConnection()->expire($key, $ttl);
50 | }
51 |
52 | return redirect(route('redis-edit-key', [
53 | 'conn' => request('conn'),
54 | 'key' => $key,
55 | ]));
56 | }
57 |
58 | /**
59 | * Remove a member from list by index.
60 | *
61 | * @param array $params
62 | *
63 | * @return mixed
64 | */
65 | public function remove(array $params)
66 | {
67 | $key = array_get($params, 'key');
68 | $index = array_get($params, 'index');
69 |
70 | $lua = <<<'LUA'
71 | redis.call('lset', KEYS[1], ARGV[1], '__DELETED__');
72 | redis.call('lrem', KEYS[1], 1, '__DELETED__');
73 | LUA;
74 |
75 | return $this->getConnection()->eval($lua, 1, $key, $index);
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/DataType/Sets.php:
--------------------------------------------------------------------------------
1 | getConnection()->smembers($key);
13 | }
14 |
15 | /**
16 | * {@inheritdoc}
17 | */
18 | public function update(array $params)
19 | {
20 | $key = array_get($params, 'key');
21 |
22 | if (array_has($params, 'member')) {
23 | $member = array_get($params, 'member');
24 | $this->getConnection()->sadd($key, $member);
25 | }
26 |
27 | if (array_has($params, '_editable')) {
28 | $new = array_get($params, 'value');
29 | $old = array_get($params, 'pk');
30 |
31 | $this->getConnection()->transaction(function ($tx) use ($key, $old, $new) {
32 | $tx->srem($key, $old);
33 | $tx->sadd($key, $new);
34 | });
35 | }
36 | }
37 |
38 | /**
39 | * {@inheritdoc}
40 | */
41 | public function store(array $params)
42 | {
43 | $key = array_get($params, 'key');
44 | $ttl = array_get($params, 'ttl');
45 | $members = array_get($params, 'members');
46 |
47 | $this->getConnection()->sadd($key, $members);
48 |
49 | if ($ttl > 0) {
50 | $this->getConnection()->expire($key, $ttl);
51 | }
52 |
53 | return redirect(route('redis-edit-key', [
54 | 'conn' => request('conn'),
55 | 'key' => $key,
56 | ]));
57 | }
58 |
59 | /**
60 | * Remove a member from a set.
61 | *
62 | * @param array $params
63 | *
64 | * @return int
65 | */
66 | public function remove(array $params)
67 | {
68 | $key = array_get($params, 'key');
69 | $member = array_get($params, 'member');
70 |
71 | return $this->getConnection()->srem($key, $member);
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/DataType/SortedSets.php:
--------------------------------------------------------------------------------
1 | getConnection()->zrange($key, 0, -1, ['WITHSCORES' => true]);
13 | }
14 |
15 | public function update(array $params)
16 | {
17 | $key = array_get($params, 'key');
18 |
19 | if (array_has($params, 'member')) {
20 | $member = array_get($params, 'member');
21 | $score = array_get($params, 'score');
22 | $this->getConnection()->zadd($key, [$member => $score]);
23 | }
24 |
25 | if (array_has($params, '_editable')) {
26 | $score = array_get($params, 'value');
27 | $member = array_get($params, 'pk');
28 |
29 | $this->getConnection()->zadd($key, [$member => $score]);
30 | }
31 | }
32 |
33 | /**
34 | * {@inheritdoc}
35 | */
36 | public function store(array $params)
37 | {
38 | $key = array_get($params, 'key');
39 | $ttl = array_get($params, 'ttl');
40 | $score = array_get($params, 'score');
41 | $member = array_get($params, 'member');
42 |
43 | $this->getConnection()->zadd($key, [$member => $score]);
44 |
45 | if ($ttl > 0) {
46 | $this->getConnection()->expire($key, $ttl);
47 | }
48 |
49 | return redirect(route('redis-edit-key', [
50 | 'conn' => request('conn'),
51 | 'key' => $key,
52 | ]));
53 | }
54 |
55 | /**
56 | * Remove a member from a sorted set.
57 | *
58 | * @param array $params
59 | *
60 | * @return int
61 | */
62 | public function remove(array $params)
63 | {
64 | $key = array_get($params, 'key');
65 | $member = array_get($params, 'member');
66 |
67 | return $this->getConnection()->zrem($key, $member);
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/DataType/Strings.php:
--------------------------------------------------------------------------------
1 | getConnection()->get($key);
13 | }
14 |
15 | /**
16 | * {@inheritdoc}
17 | */
18 | public function update(array $params)
19 | {
20 | $this->store($params);
21 | }
22 |
23 | /**
24 | * {@inheritdoc}
25 | */
26 | public function store(array $params)
27 | {
28 | $key = array_get($params, 'key');
29 | $value = array_get($params, 'value');
30 | $ttl = array_get($params, 'ttl');
31 |
32 | $this->getConnection()->set($key, $value);
33 |
34 | if ($ttl > 0) {
35 | $this->getConnection()->expire($key, $ttl);
36 | }
37 |
38 | return redirect(route('redis-index', [
39 | 'conn' => request('conn'),
40 | ]));
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/RedisController.php:
--------------------------------------------------------------------------------
1 | header('Redis manager');
22 | $content->description('Connections');
23 | $content->breadcrumb(['text' => 'Redis manager']);
24 |
25 | $connection = request('conn', 'default');
26 |
27 | $manager = $this->manager();
28 | //dd($manager->getConnections());
29 | $variables = [
30 | 'conn' => $connection,
31 | 'info' => $manager->getInformation(),
32 | 'connections' => $manager->getConnections(),
33 | 'keys' => $manager->scan(
34 | request('pattern', '*'),
35 | request('count', 50)
36 | ),
37 | ];
38 |
39 | $content->body(view('laravel-admin-redis-manager::index', $variables));
40 | });
41 | }
42 |
43 | /**
44 | * Edit page.
45 | *
46 | * @param Request $request
47 | *
48 | * @return Content
49 | */
50 | public function edit(Request $request)
51 | {
52 | return Admin::content(function (Content $content) use ($request) {
53 | $connection = $request->get('conn', 'default');
54 |
55 | $manager = $this->manager();
56 |
57 | $variables = [
58 | 'conn' => $connection,
59 | 'info' => $manager->getInformation(),
60 | 'connections' => $manager->getConnections(),
61 | 'data' => $manager->fetch($request->get('key')),
62 | ];
63 |
64 | if (empty($variables['data'])) {
65 | $view = 'laravel-admin-redis-manager::edit.nil';
66 | } else {
67 | $view = 'laravel-admin-redis-manager::edit.'.$variables['data']['type'];
68 | }
69 |
70 | $content->header('Redis manager');
71 | $content->description('Connections');
72 | $content->breadcrumb(
73 | ['text' => 'Redis manager', 'url' => route('redis-index', ['conn' => $connection])],
74 | ['text' => 'Edit']
75 | );
76 | $content->body(view($view, $variables));
77 | });
78 | }
79 |
80 | /**
81 | * Create page.
82 | *
83 | * @param Request $request
84 | *
85 | * @return Content
86 | */
87 | public function create(Request $request)
88 | {
89 | return Admin::content(function (Content $content) use ($request) {
90 | $connection = $request->get('conn', 'default');
91 |
92 | $manager = $this->manager();
93 |
94 | $vars = [
95 | 'conn' => $connection,
96 | 'info' => $manager->getInformation(),
97 | 'connections' => $manager->getConnections(),
98 | 'type' => $request->get('type'),
99 | ];
100 |
101 | $view = 'laravel-admin-redis-manager::create.'.$vars['type'];
102 |
103 | $content->header('Redis manager');
104 | $content->description('Connections');
105 | $content->breadcrumb(
106 | ['text' => 'Redis manager', 'url' => route('redis-index', ['conn' => $connection])],
107 | ['text' => 'Create']
108 | );
109 | $content->body(view($view, $vars));
110 | });
111 | }
112 |
113 | /**
114 | * @param Request $request
115 | *
116 | * @return mixed
117 | */
118 | public function store(Request $request)
119 | {
120 | $type = $request->get('type');
121 |
122 | return $this->manager()->{$type}()->store($request->all());
123 | }
124 |
125 | /**
126 | * @param Request $request
127 | *
128 | * @return int
129 | */
130 | public function destroy(Request $request)
131 | {
132 | return $this->manager()->del($request->get('key'));
133 | }
134 |
135 | /**
136 | * @param Request $request
137 | *
138 | * @return array
139 | */
140 | public function fetch(Request $request)
141 | {
142 | return $this->manager()->fetch($request->get('key'));
143 | }
144 |
145 | /**
146 | * @param Request $request
147 | *
148 | * @return mixed
149 | */
150 | public function remove(Request $request)
151 | {
152 | $type = $request->get('type');
153 |
154 | return $this->manager()->{$type}()->remove($request->all());
155 | }
156 |
157 | /**
158 | * @param Request $request
159 | *
160 | * @return mixed
161 | */
162 | public function update(Request $request)
163 | {
164 | return $this->manager()->update($request);
165 | }
166 |
167 | /**
168 | * Redis console interface.
169 | *
170 | * @param Request $request
171 | *
172 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
173 | */
174 | public function console(Request $request)
175 | {
176 | return Admin::content(function (Content $content) use ($request) {
177 | $connection = $request->get('conn', 'default');
178 |
179 | $manager = $this->manager();
180 |
181 | $vars = [
182 | 'conn' => $connection,
183 | 'info' => $manager->getInformation(),
184 | 'connections' => $manager->getConnections(),
185 | ];
186 |
187 | $view = 'laravel-admin-redis-manager::console';
188 |
189 | $content->header('Redis manager');
190 | $content->description('Connections');
191 | $content->breadcrumb(
192 | ['text' => 'Redis manager', 'url' => route('redis-index', ['conn' => $connection])],
193 | ['text' => 'Console']
194 | );
195 | $content->body(view($view, $vars));
196 | });
197 | }
198 |
199 | /**
200 | * Execute a redis command.
201 | *
202 | * @param Request $request
203 | *
204 | * @return bool|string
205 | */
206 | public function execute(Request $request)
207 | {
208 | $command = $request->get('command');
209 |
210 | try {
211 | $result = $this->manager()->execute($command);
212 | } catch (\Exception $exception) {
213 | return $this->renderException($exception);
214 | }
215 |
216 | if (is_string($result) && Str::startsWith($result, ['ERR ', 'WRONGTYPE '])) {
217 | return $this->renderException(new \Exception($result));
218 | }
219 |
220 | return $this->getDumpedHtml($result);
221 | }
222 |
223 | /**
224 | * Render exception.
225 | *
226 | * @param \Exception $exception
227 | *
228 | * @return string
229 | */
230 | protected function renderException(\Exception $exception)
231 | {
232 | return sprintf(
233 | " %s
",
234 | str_replace("\n", '
', $exception->getMessage())
235 | );
236 | }
237 |
238 | /**
239 | * Get html of dumped variable.
240 | *
241 | * @param mixed $var
242 | *
243 | * @return bool|string
244 | */
245 | protected function getDumpedHtml($var)
246 | {
247 | ob_start();
248 |
249 | dump($var);
250 |
251 | $content = ob_get_contents();
252 |
253 | ob_get_clean();
254 |
255 | return substr($content, strpos($content, 'get('conn');
266 |
267 | return RedisManager::instance($conn);
268 | }
269 | }
270 |
--------------------------------------------------------------------------------
/src/RedisManager.php:
--------------------------------------------------------------------------------
1 | 'primary',
32 | 'list' => 'info',
33 | 'zset' => 'danger',
34 | 'hash' => 'warning',
35 | 'set' => 'success',
36 | ];
37 |
38 | /**
39 | * @var array
40 | */
41 | protected $dataTyps = [
42 | 'string' => Strings::class,
43 | 'hash' => Hashes::class,
44 | 'set' => Sets::class,
45 | 'zset' => SortedSets::class,
46 | 'list' => Lists::class,
47 | ];
48 |
49 | /**
50 | * @var RedisManager
51 | */
52 | protected static $instance;
53 |
54 | /**
55 | * @var string
56 | */
57 | protected $connection;
58 |
59 | /**
60 | * Get instance of redis manager.
61 | *
62 | * @param string $connection
63 | *
64 | * @return RedisManager
65 | */
66 | public static function instance($connection = 'default')
67 | {
68 | if (!static::$instance instanceof self) {
69 | static::$instance = new static($connection);
70 | }
71 |
72 | return static::$instance;
73 | }
74 |
75 | /**
76 | * RedisManager constructor.
77 | *
78 | * @param string $connection
79 | */
80 | public function __construct($connection = 'default')
81 | {
82 | $this->connection = $connection;
83 | }
84 |
85 | /**
86 | * @return Lists
87 | */
88 | public function list()
89 | {
90 | return new Lists($this->getConnection());
91 | }
92 |
93 | /**
94 | * @return Strings
95 | */
96 | public function string()
97 | {
98 | return new Strings($this->getConnection());
99 | }
100 |
101 | /**
102 | * @return Hashes
103 | */
104 | public function hash()
105 | {
106 | return new Hashes($this->getConnection());
107 | }
108 |
109 | /**
110 | * @return Sets
111 | */
112 | public function set()
113 | {
114 | return new Sets($this->getConnection());
115 | }
116 |
117 | /**
118 | * @return SortedSets
119 | */
120 | public function zset()
121 | {
122 | return new SortedSets($this->getConnection());
123 | }
124 |
125 | /**
126 | * Get connection collections.
127 | *
128 | * @return Collection
129 | */
130 | public function getConnections()
131 | {
132 | return collect(config('database.redis'))->filter(function ($conn) {
133 | return is_array($conn);
134 | });
135 | }
136 |
137 | /**
138 | * Get a registered connection instance.
139 | *
140 | * @param string $connection
141 | *
142 | * @return Connection
143 | */
144 | public function getConnection($connection = null)
145 | {
146 | if ($connection) {
147 | $this->connection = $connection;
148 | }
149 |
150 | return Redis::connection($this->connection);
151 | }
152 |
153 | /**
154 | * Get information of redis instance.
155 | *
156 | * @return array
157 | */
158 | public function getInformation()
159 | {
160 | return $this->getConnection()->info();
161 | }
162 |
163 | /**
164 | * Scan keys in redis by giving pattern.
165 | *
166 | * @param string $pattern
167 | * @param int $count
168 | *
169 | * @return array|\Predis\Pipeline\Pipeline
170 | */
171 | public function scan($pattern = '*', $count = 100)
172 | {
173 | $client = $this->getConnection();
174 | $keys = [];
175 |
176 | foreach (new Keyspace($client->client(), $pattern) as $item) {
177 | $keys[] = $item;
178 |
179 | if (count($keys) == $count) {
180 | break;
181 | }
182 | }
183 |
184 | $script = <<<'LUA'
185 | local type = redis.call('type', KEYS[1])
186 | local ttl = redis.call('ttl', KEYS[1])
187 |
188 | return {KEYS[1], type, ttl}
189 | LUA;
190 |
191 | return $client->pipeline(function (Pipeline $pipe) use ($keys, $script) {
192 | foreach ($keys as $key) {
193 | $pipe->eval($script, 1, $key);
194 | }
195 | });
196 | }
197 |
198 | /**
199 | * Fetch value of a giving key.
200 | *
201 | * @param string $key
202 | *
203 | * @return array
204 | */
205 | public function fetch($key)
206 | {
207 | if (!$this->getConnection()->exists($key)) {
208 | return [];
209 | }
210 |
211 | $type = $this->getConnection()->type($key)->__toString();
212 |
213 | /** @var DataType $class */
214 | $class = $this->{$type}();
215 |
216 | $value = $class->fetch($key);
217 | $ttl = $class->ttl($key);
218 |
219 | return compact('key', 'value', 'ttl', 'type');
220 | }
221 |
222 | /**
223 | * Update a specified key.
224 | *
225 | * @param Request $request
226 | *
227 | * @return bool
228 | */
229 | public function update(Request $request)
230 | {
231 | $key = $request->get('key');
232 | $type = $request->get('type');
233 |
234 | /** @var DataType $class */
235 | $class = $this->{$type}();
236 |
237 | $class->update($request->all());
238 |
239 | $class->setTtl($key, $request->get('ttl'));
240 | }
241 |
242 | /**
243 | * Remove the specified key.
244 | *
245 | * @param string $key
246 | *
247 | * @return int
248 | */
249 | public function del($key)
250 | {
251 | if (is_string($key)) {
252 | $key = [$key];
253 | }
254 |
255 | return $this->getConnection()->del($key);
256 | }
257 |
258 | /**
259 | * 运行redis命令.
260 | *
261 | * @param string $command
262 | *
263 | * @return mixed
264 | */
265 | public function execute($command)
266 | {
267 | $command = explode(' ', $command);
268 |
269 | return $this->getConnection()->executeRaw($command);
270 | }
271 |
272 | /**
273 | * @param string $type
274 | *
275 | * @return mixed
276 | */
277 | public static function typeColor($type)
278 | {
279 | return Arr::get(static::$typeColor, $type, 'default');
280 | }
281 | }
282 |
--------------------------------------------------------------------------------
/src/RedisManagerServiceProvider.php:
--------------------------------------------------------------------------------
1 | loadViewsFrom(__DIR__.'/../resources/views', 'laravel-admin-redis-manager');
15 |
16 | RedisManager::boot();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------