問(wèn)題描述
我已經(jīng)開(kāi)始使用 Laravel.工作很有趣.我已經(jīng)開(kāi)始使用 Laravel 的功能了.我已經(jīng)開(kāi)始使用 redis
通過(guò)在我的系統(tǒng)中安裝 redis 服務(wù)器并更改 app/config/database.php
文件中的 redis 配置.通過(guò)使用 set
,redis 可以很好地處理單個(gè)變量.即,
$redis = Redis::connection();$redis->set('name', 'Test');
我可以通過(guò)使用獲得價(jià)值
$redis->get('name');
但我想使用 set
函數(shù)來(lái)設(shè)置數(shù)組.如果我嘗試這樣做,則會(huì)出現(xiàn)以下錯(cuò)誤
strlen() 期望參數(shù) 1 是字符串,給定數(shù)組
我已嘗試使用以下代碼.
$redis->set('name', array(5, 10));$values = $redis->lrange('names', array(5, 10));
如果我使用
$values = $redis->command('lrange', array(5, 10));
出現(xiàn)以下錯(cuò)誤
'command' 不是注冊(cè)的 Redis 命令
任何人都可以向我解釋這個(gè)問(wèn)題嗎?redis 可以嗎?...我們可以使用 redis
設(shè)置數(shù)組值嗎?
這已經(jīng)在評(píng)論中回答了,但為了讓以后訪問(wèn)的人更清楚地回答.
Redis 與語(yǔ)言無(wú)關(guān),因此它不會(huì)識(shí)別特定于 PHP 或任何其他語(yǔ)言的任何數(shù)據(jù)類(lèi)型.最簡(jiǎn)單的方法是 serialise
/json_encode
數(shù)據(jù)集,然后 unserialise
/json_decode
獲取.>
使用 json_encode
存儲(chǔ)數(shù)據(jù)的示例:
使用 IlluminateSupportFacadesRedis;$redis = Redis::connection();$redis->set('user_details', json_encode(['first_name' =>'亞歷克斯','姓氏' =>理查茲"]));
使用 json_decode
檢索數(shù)據(jù)的示例:
使用 IlluminateSupportFacadesRedis;$redis = Redis::connection();$response = $redis->get('user_details');$response = json_decode($response);
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.,
$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
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
?
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.
Example to store data using json_encode
:
use IlluminateSupportFacadesRedis;
$redis = Redis::connection();
$redis->set('user_details', json_encode([
'first_name' => 'Alex',
'last_name' => 'Richards'
])
);
Example to retrieve data using json_decode
:
use IlluminateSupportFacadesRedis;
$redis = Redis::connection();
$response = $redis->get('user_details');
$response = json_decode($response);
這篇關(guān)于使用 Redis 存儲(chǔ)數(shù)據(jù)數(shù)組(來(lái)自 Laravel)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!