php 使用 Redis 存储数据数组(来自 Laravel)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22718903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing an array of data using Redis (from Laravel)
提问by Kalai
I have started to work with laravel. It is quite interesting to work. I have started to use the features of laravel. I have started to use redis
by install redis server in my system and change the configuration for redis in app/config/database.php
file. The redis is working fine for the single variables by using set
. i.e.,
我已经开始使用 Laravel 了。工作很有趣。我已经开始使用 Laravel 的功能了。我已经开始使用redis
通过在我的系统中安装 redis 服务器并更改app/config/database.php
文件中的redis 配置。通过使用 .redis 可以很好地处理单个变量set
。IE,
$redis = Redis::connection();
$redis->set('name', 'Test');
and i could able to get the value by using
我可以通过使用获得价值
$redis->get('name');
But i want to set the array by using set
function. If i try do that getting the following error
但我想通过使用set
函数来设置数组。如果我尝试这样做会收到以下错误
strlen() expects parameter 1 to be string, array given
I have tried by using following codes.
我尝试使用以下代码。
$redis->set('name', array(5, 10));
$values = $redis->lrange('names', array(5, 10));
and if i use
如果我使用
$values = $redis->command('lrange', array(5, 10));
getting the following error
收到以下错误
'command' is not a registered Redis command
Can any one explain me the problem and is that possible with redis?...we can set the array values using redis
?
任何人都可以向我解释这个问题,redis 可以吗?...我们可以使用redis
?
回答by ajtrichards
This has been answered in the comments but to make the answer clearer for people visiting in the future.
这已在评论中回答,但为了让将来访问的人更清楚答案。
Redis is language agnostic so it won't recognise any datatype specific to PHP or any other language. The easiest way would be to serialise
/ json_encode
the data on set then unserialise
/json_decode
on get.
Redis 与语言无关,因此它不会识别特定于 PHP 或任何其他语言的任何数据类型。最简单的方法是serialise
/json_encode
设置然后unserialise
/json_decode
获取数据。
Example to store data using json_encode
:
使用json_encode
以下方法存储数据的示例:
use Illuminate\Support\Facades\Redis;
$redis = Redis::connection();
$redis->set('user_details', json_encode([
'first_name' => 'Alex',
'last_name' => 'Richards'
])
);
Example to retrieve data using json_decode
:
使用json_decode
以下方法检索数据的示例:
use Illuminate\Support\Facades\Redis;
$redis = Redis::connection();
$response = $redis->get('user_details');
$response = json_decode($response);
回答by Abilogos
Redis Supports Multiple Data Structure Types Like Hash, Link List And Array.
Redis 支持多种数据结构类型,如哈希、链表和数组。
But You Have to User Proper Methods from Laravael Redis Facade Like:
但是您必须使用 Laravael Redis Facade 中的正确方法,例如:
//set user_details key and below key_values
Redis::hmset('user_details',["firstName" => "Abi", "lastName" => "logos"]);
//get all as an Associate Array
Redis::hgetall('user_details');
//get just The Keys as an Array
Redis::hkeys('user_details');
further information: https://redis.io/commands#hash
回答by Seif
In your controller, you can add such function:
在您的控制器中,您可以添加这样的功能:
use Illuminate\Support\Facades\Redis;
private function getUsers() {
if ($users = Redis::get('users.all')) {
return json_decode($users);
}
$users = User::all();
Redis::set('users.all', $users);
return $users;
}
Then in your index
method (or others) you can do this:
然后在您的index
方法(或其他方法)中,您可以执行以下操作:
public function index(Request $request) {
$users = $this->getUsers();
}
Source: accepted answer and this.
来源:接受的答案和这个。