1. <i id='4KZNe'><tr id='4KZNe'><dt id='4KZNe'><q id='4KZNe'><span id='4KZNe'><b id='4KZNe'><form id='4KZNe'><ins id='4KZNe'></ins><ul id='4KZNe'></ul><sub id='4KZNe'></sub></form><legend id='4KZNe'></legend><bdo id='4KZNe'><pre id='4KZNe'><center id='4KZNe'></center></pre></bdo></b><th id='4KZNe'></th></span></q></dt></tr></i><div id='4KZNe'><tfoot id='4KZNe'></tfoot><dl id='4KZNe'><fieldset id='4KZNe'></fieldset></dl></div>
  2. <tfoot id='4KZNe'></tfoot>
  3. <small id='4KZNe'></small><noframes id='4KZNe'>

        <bdo id='4KZNe'></bdo><ul id='4KZNe'></ul>
      <legend id='4KZNe'><style id='4KZNe'><dir id='4KZNe'><q id='4KZNe'></q></dir></style></legend>

      使用 Redis 存储数据数组(来自 Laravel)

      时间:2023-10-04

        • <bdo id='2NjOH'></bdo><ul id='2NjOH'></ul>

          <small id='2NjOH'></small><noframes id='2NjOH'>

        • <tfoot id='2NjOH'></tfoot>
            <legend id='2NjOH'><style id='2NjOH'><dir id='2NjOH'><q id='2NjOH'></q></dir></style></legend>
            <i id='2NjOH'><tr id='2NjOH'><dt id='2NjOH'><q id='2NjOH'><span id='2NjOH'><b id='2NjOH'><form id='2NjOH'><ins id='2NjOH'></ins><ul id='2NjOH'></ul><sub id='2NjOH'></sub></form><legend id='2NjOH'></legend><bdo id='2NjOH'><pre id='2NjOH'><center id='2NjOH'></center></pre></bdo></b><th id='2NjOH'></th></span></q></dt></tr></i><div id='2NjOH'><tfoot id='2NjOH'></tfoot><dl id='2NjOH'><fieldset id='2NjOH'></fieldset></dl></div>

              <tbody id='2NjOH'></tbody>

                本文介绍了使用 Redis 存储数据数组(来自 Laravel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我已经开始使用 Laravel.工作很有趣.我已经开始使用 Laravel 的功能了.我已经开始使用 redis 通过在我的系统中安装 redis 服务器并更改 app/config/database.php 文件中的 redis 配置.通过使用 set,redis 可以很好地处理单个变量.即,

                $redis = Redis::connection();$redis->set('name', 'Test');

                我可以通过使用获得价值

                $redis->get('name');

                但我想使用 set 函数来设置数组.如果我尝试这样做,则会出现以下错误

                 strlen() 期望参数 1 是字符串,给定数组

                我已尝试使用以下代码.

                $redis->set('name', array(5, 10));$values = $redis->lrange('names', array(5, 10));

                如果我使用

                $values = $redis->command('lrange', array(5, 10));

                出现以下错误

                 'command' 不是注册的 Redis 命令

                任何人都可以向我解释这个问题吗?redis 可以吗?...我们可以使用 redis 设置数组值吗?

                解决方案

                这已经在评论中回答了,但为了让以后访问的人更清楚地回答.

                Redis 与语言无关,因此它不会识别特定于 PHP 或任何其他语言的任何数据类型.最简单的方法是 serialise/json_encode 数据集,然后 unserialise/json_decode 获取.

                使用 json_encode 存储数据的示例:

                使用 IlluminateSupportFacadesRedis;$redis = Redis::connection();$redis->set('user_details', json_encode(['first_name' =>'亚历克斯','姓氏' =>理查兹"]));

                使用 json_decode 检索数据的示例:

                使用 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);
                

                这篇关于使用 Redis 存储数据数组(来自 Laravel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:安装 phpredis MAC OSX 下一篇:phpMyAdmin 相当于 MySQL for Redis 吗?

                相关文章

                最新文章

                <small id='bfbUN'></small><noframes id='bfbUN'>

                  <i id='bfbUN'><tr id='bfbUN'><dt id='bfbUN'><q id='bfbUN'><span id='bfbUN'><b id='bfbUN'><form id='bfbUN'><ins id='bfbUN'></ins><ul id='bfbUN'></ul><sub id='bfbUN'></sub></form><legend id='bfbUN'></legend><bdo id='bfbUN'><pre id='bfbUN'><center id='bfbUN'></center></pre></bdo></b><th id='bfbUN'></th></span></q></dt></tr></i><div id='bfbUN'><tfoot id='bfbUN'></tfoot><dl id='bfbUN'><fieldset id='bfbUN'></fieldset></dl></div>
                  <tfoot id='bfbUN'></tfoot>

                  1. <legend id='bfbUN'><style id='bfbUN'><dir id='bfbUN'><q id='bfbUN'></q></dir></style></legend>
                      <bdo id='bfbUN'></bdo><ul id='bfbUN'></ul>