{"id":245,"date":"2020-12-23T00:45:55","date_gmt":"2020-12-23T00:45:55","guid":{"rendered":"http:\/\/robinluo.top\/?p=245"},"modified":"2020-12-24T09:36:25","modified_gmt":"2020-12-24T09:36:25","slug":"laravel-redis%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/robinluo.top\/?p=245","title":{"rendered":"Laravel redis\u4f7f\u7528"},"content":{"rendered":"\n<p>laravel \u4f7f\u7528redis \u9700\u8981\u5148\u5b89\u88c5redis\u5305<\/p>\n\n\n\n<p>composer require predis\/predis<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>redis\u7684\u914d\u7f6e\u6587\u4ef6\u662f\uff1aconfig\/database.php\n \n 'redis' => &#91;\n \n        'client' => 'predis', \/\/\u6307\u5b9aredis\u9a71\u52a8\u4e3apredis\n \n        'default' => &#91;\n            'host' => env('REDIS_HOST', '127.0.0.1'),\n            'password' => env('REDIS_PASSWORD',null),\n            'port' => env('REDIS_PORT', 6379),\n            'database' => 0,\n        ],\n \n    ],<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\u5728 .env \u914d\u7f6e\u5177\u4f53\u53c2\u6570\nREDIS_CLIENT=predis\n    \/\/\u4e5f\u53ef\u5728\u8fd9\u91cc\u6307\u5b9aredis\u9a71\u52a8\u4e3apredis\nREDIS_HOST=127.0.0.1\nREDIS_PASSWORD=null\nREDIS_PORT=6379<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\u64cd\u4f5c\u4ee3\u7801\n\nuse Illuminate\\Support\\Facades\\Redis;\n\n\/\/set\u5b58\u6570\u636e \u521b\u5efa\u4e00\u4e2a key \u5e76\u8bbe\u7f6evalue \nRedis::set('key','value'); \n \n\/\/get\u547d\u4ee4\u7528\u4e8e\u83b7\u53d6\u6307\u5b9a key \u7684\u503c,key\u4e0d\u5b58\u5728,\u8fd4\u56denull,\u5982\u679ckey\u50a8\u5b58\u7684\u503c\u4e0d\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef\u3002\nvar_dump(Redis::get('key'));\n \n\/\/del \u5220\u9664 \u6210\u529f\u5220\u9664\u8fd4\u56de true, \u5931\u8d25\u5219\u8fd4\u56de false\nRedis::del('key');\n \n\/\/mset\u5b58\u50a8\u591a\u4e2a key \u5bf9\u5e94\u7684 value\n$array= array(\n\t\t'user1'=>'\u5f20\u4e09',\n\t\t'user2'=>'\u674e\u56db',\n\t\t'user3'=>'\u738b\u4e94'\n);\nredis::mset($array); \/\/ \u5b58\u50a8\u591a\u4e2a key \u5bf9\u5e94\u7684 value\n \n\/\/ Mget\u8fd4\u56de\u6240\u6709(\u4e00\u4e2a\u6216\u591a\u4e2a)\u7ed9\u5b9a key \u7684\u503c,\u7ed9\u5b9a\u7684 key \u91cc\u9762,key \u4e0d\u5b58\u5728,\u8fd9\u4e2a key \u8fd4\u56de\u7279\u6b8a\u503c nil\n \nvar_dump(redis::mget (array_keys( $array))); \/\/\u83b7\u53d6\u591a\u4e2akey\u5bf9\u5e94\u7684value\n \n\/\/Strlen \u547d\u4ee4\u7528\u4e8e\u83b7\u53d6\u6307\u5b9a key \u6240\u50a8\u5b58\u7684\u5b57\u7b26\u4e32\u503c\u7684\u957f\u5ea6\u3002\u5f53 key\u5b58\u50a8\u4e0d\u662f\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u9519\u8bef\u3002\nvar_dump(redis::strlen('key'));\n \n\/\/substr \u83b7\u53d6\u7b2c\u4e00\u5230\u7b2c\u4e09\u4f4d\u5b57\u7b26\nvar_dump(Redis::substr('key',0,2));\n \n\/\/\u6839\u636e\u952e\u540d\u6a21\u7cca\u641c\u7d22\nvar_dump(Redis::keys('use*'));\/\/\u6a21\u7cca\u641c\u7d22\n \n\/\/\u83b7\u53d6\u7f13\u5b58\u65f6\u95f4\nRedis::ttl('str2');\/\/\u83b7\u53d6\u7f13\u5b58\u65f6\u95f4\n \n\/\/exists\u68c0\u6d4b\u662f\u5426\u5b58\u5728\u67d0\u503c\nRedis::exists ( 'foo' ) ; \/\/true\n<\/code><\/pre>\n\n\n\n<p>redis get<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> 'user' => Redis::get('user:profile:'.$id)<\/code><\/pre>\n\n\n\n<p>redis lrange<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$values = Redis::lrange('names', 5, 10);<\/code><\/pre>\n\n\n\n<p>\u547d\u4ee4\u6a21\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$values = Redis::command('lrange', &#91;'name', 5, 10]);<\/code><\/pre>\n\n\n\n<p>\u6307\u5b9a\u8fde\u63a5\u8fd4\u56deredis\u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$redis = Redis::connection('connection-name');<\/code><\/pre>\n\n\n\n<p>redis \u4e8b\u52a1<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nRedis::transaction(function ($redis) {\n    $redis->incr('user_visits', 1);\n    $redis->incr('total_visits', 1);\n});<\/code><\/pre>\n\n\n\n<p>pipeline\u6a21\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Redis::pipeline(function ($pipe) {\n    for ($i = 0; $i &lt; 1000; $i++) {\n        $pipe->set(\"key:$i\", $i);\n    }\n});<\/code><\/pre>\n\n\n\n<p>redis\u6d88\u606f\u961f\u5217(\u7528laravel command \u751f\u6210\u76d1\u542c\u7c7b)<\/p>\n\n\n\n<p>\u6d88\u606f\u8ba2\u9605<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace App\\Console\\Commands;\n\nuse Illuminate\\Console\\Command;\nuse Illuminate\\Support\\Facades\\Redis;\n\nclass RedisSubscribe extends Command\n{\n    \/**\n     * The name and signature of the console command.\n     *\n     * @var string\n     *\/\n    protected $signature = 'redis:subscribe';\n\n    \/**\n     * The console command description.\n     *\n     * @var string\n     *\/\n    protected $description = 'Subscribe to a Redis channel';\n\n    \/**\n     * Execute the console command.\n     *\n     * @return mixed\n     *\/\n    public function handle()\n    {\n        Redis::subscribe(&#91;'test-channel'], function ($message) {\n            echo $message;\n        });\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u6d88\u606f\u53d1\u5e03<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> Redis::publish('test-channel', json_encode(&#91;\n        'name' => 'Adam Wathan'\n    ]));<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>laravel \u4f7f\u7528redis \u9700\u8981\u5148\u5b89\u88c5redis\u5305 composer require predis\/predis redis get redis lrange \u547d\u4ee4\u6a21\u5f0f \u6307\u5b9a\u8fde\u63a5\u8fd4\u56deredis\u5bf9\u8c61 redis \u4e8b\u52a1 pipeline\u6a21\u5f0f redis\u6d88\u606f\u961f\u5217(\u7528laravel command \u751f\u6210\u76d1\u542c\u7c7b) \u6d88\u606f\u8ba2\u9605 \u6d88\u606f\u53d1\u5e03<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[10,25],"_links":{"self":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/245"}],"collection":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=245"}],"version-history":[{"count":5,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/245\/revisions"}],"predecessor-version":[{"id":282,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/245\/revisions\/282"}],"wp:attachment":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}