Kal*_*lai 20 php redis laravel
我已经开始使用laravel了.工作很有意思.我已经开始使用laravel的功能.我已经开始redis在我的系统中使用安装redis服务器并在app/config/database.php文件中更改redis的配置.通过使用,redis对单个变量工作正常set.即
$redis = Redis::connection();
$redis->set('name', 'Test');
Run Code Online (Sandbox Code Playgroud)
我可以通过使用获得价值
$redis->get('name');
Run Code Online (Sandbox Code Playgroud)
但我想通过使用set函数来设置数组.如果我尝试这样做会得到以下错误
strlen() expects parameter 1 to be string, array given
Run Code Online (Sandbox Code Playgroud)
我尝试过使用以下代码.
$redis->set('name', array(5, 10));
$values = $redis->lrange('names', array(5, 10));
Run Code Online (Sandbox Code Playgroud)
如果我使用
$values = $redis->command('lrange', array(5, 10));
Run Code Online (Sandbox Code Playgroud)
得到以下错误
'command' is not a registered Redis command
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释我的问题,并且可以使用redis吗?...我们可以使用redis?设置数组值?
ajt*_*rds 46
评论中已经回答了这个问题,但为将来访问的人们提供了更清楚的答案.
Redis与语言无关,因此它无法识别PHP或任何其他语言特有的任何数据类型.最简单的方法是serialise/ json_encodeon set unserialise/ then json_decodeget 上的数据.
用于存储数据的示例json_encode:
use Illuminate\Support\Facades\Redis;
$redis = Redis::connection();
$redis->set('user_details', json_encode([
'first_name' => 'Alex',
'last_name' => 'Richards'
])
);
Run Code Online (Sandbox Code Playgroud)
使用json_decode以下方法检索数据
use Illuminate\Support\Facades\Redis;
$redis = Redis::connection();
$response = $redis->get('user_details');
$response = json_decode($response);
Run Code Online (Sandbox Code Playgroud)
Redis 支持多种数据结构类型,如hash、link list和array。
但是您必须使用Laravel Redis 门面中的正确方法,如下所示:
// set user_details --redis_key-- and --redis_value-- as array of key_values
Redis::hmset('user_details',["firstName" => "Foo", "lastName" => "Bar"]);
// get all as an associative array
Redis::hgetall('user_details');
// get just the keys as an array
Redis::hkeys('user_details');
Run Code Online (Sandbox Code Playgroud)
更多信息:https : //redis.io/commands#hash
| 归档时间: |
|
| 查看次数: |
25940 次 |
| 最近记录: |