{"id":847,"date":"2025-06-12T01:02:57","date_gmt":"2025-06-11T17:02:57","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/laravel\/847.html"},"modified":"2025-06-12T01:02:57","modified_gmt":"2025-06-11T17:02:57","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8laravel%e4%b8%ad%e5%ad%98%e5%82%a8%e5%92%8c%e8%af%bb%e5%8f%96%e7%bc%93%e5%ad%98%e6%95%b0%e6%8d%ae","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/847.html","title":{"rendered":"\u5982\u4f55\u5728Laravel\u4e2d\u5b58\u50a8\u548c\u8bfb\u53d6\u7f13\u5b58\u6570\u636e"},"content":{"rendered":"<div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--1\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/dyA-1-400x300.jpg\" alt=\"Bug&#32534;&#35793;&#29422;\" class=\"wp-image-1842\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">Bug&#32534;&#35793;&#29422;<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#22312;Laravel&#20013;&#65292;&#32531;&#23384;&#26159;&#19968;&#31181;&#38750;&#24120;&#24378;&#22823;&#30340;&#24037;&#20855;&#65292;&#21487;&#20197;&#29992;&#26469;&#25552;&#39640;&#24212;&#29992;&#30340;&#24615;&#33021;&#12290;&#36890;&#36807;&#20351;&#29992;&#32531;&#23384;&#65292;&#20320;&#21487;&#20197;&#23558;&#26114;&#36149;&#30340;&#25805;&#20316;&#32467;&#26524;&#23384;&#20648;&#22312;&#20869;&#23384;&#20013;&#65292;&#20174;&#32780;&#36991;&#20813;&#37325;&#22797;&#35745;&#31639;&#12290;&#20197;&#19979;&#26159;&#22914;&#20309;&#22312;Laravel&#20013;&#23384;&#20648;&#21644;&#35835;&#21462;&#32531;&#23384;&#25968;&#25454;&#30340;&#22522;&#26412;&#27493;&#39588;&#12290;<\/p>\n<h3>1. &#23433;&#35013;&#24182;&#37197;&#32622;&#32531;&#23384;<\/h3>\n<p>&#39318;&#20808;&#65292;&#30830;&#20445;&#20320;&#24050;&#32463;&#23433;&#35013;&#20102;Laravel&#30340;&#32531;&#23384;&#32452;&#20214;&#12290;&#22914;&#26524;&#20320;&#36824;&#27809;&#26377;&#23433;&#35013;&#65292;&#21487;&#20197;&#36890;&#36807;Composer&#26469;&#23433;&#35013;&#65306;<\/p>\n<pre><code class=\"language-bash\">composer require illuminate\/cache<\/code><\/pre>\n<p>&#28982;&#21518;&#65292;&#20320;&#38656;&#35201;&#22312;<code>config\/app.php<\/code>&#25991;&#20214;&#20013;&#21551;&#29992;&#32531;&#23384;&#26381;&#21153;&#25552;&#20379;&#32773;&#65306;<\/p>\n<pre><code class=\"language-php\">'providers' =&gt; [\n    \/\/ ...\n    IlluminateCacheCacheServiceProvider::class,\n],<\/code><\/pre>\n<p>&#25509;&#19979;&#26469;&#65292;&#37197;&#32622;&#32531;&#23384;&#39537;&#21160;&#12290;&#40664;&#35748;&#24773;&#20917;&#19979;&#65292;Laravel&#20250;&#20351;&#29992;&#25968;&#25454;&#24211;&#20316;&#20026;&#32531;&#23384;&#39537;&#21160;&#12290;&#20320;&#21487;&#20197;&#22312;<code>config\/cache.php<\/code>&#25991;&#20214;&#20013;&#36827;&#34892;&#37197;&#32622;&#65306;<\/p>\n<pre><code class=\"language-php\">return [\n    'default' =&gt; env('CACHE_DRIVER', 'database'),\n\n    'stores' =&gt; [\n        'file' =&gt; [\n            'driver' =&gt; 'file',\n            'path' =&gt; storage_path('framework\/cache'),\n        ],\n        'redis' =&gt; [\n            'driver' =&gt; 'redis',\n            'connection' =&gt; env('REDIS_CONNECTION', 'default'),\n        ],\n        'database' =&gt; [\n            'driver' =&gt; 'database',\n            'table' =&gt; 'cache',\n        ],\n    ],\n\n    'prefix' =&gt; env('CACHE_PREFIX', null),\n];<\/code><\/pre>\n<h3>2. &#23384;&#20648;&#32531;&#23384;&#25968;&#25454;<\/h3>\n<p>&#35201;&#21521;&#32531;&#23384;&#20013;&#23384;&#20648;&#25968;&#25454;&#65292;&#21487;&#20197;&#20351;&#29992;<code>Cache<\/code> facade&#25552;&#20379;&#30340;&#26041;&#27861;&#12290;&#20197;&#19979;&#26159;&#19968;&#20010;&#31616;&#21333;&#30340;&#31034;&#20363;&#65306;<\/p>\n<pre><code class=\"language-php\">use IlluminateSupportFacadesCache;\n\n\/\/ &#23384;&#20648;&#25968;&#25454;&#21040;&#32531;&#23384;\n$userId = 1;\n$userName = 'John Doe';\nCache::put('user:{$userId}', $userName, 60); \/\/ 60&#31186;&#21518;&#36807;&#26399;\n\n\/\/ &#33719;&#21462;&#32531;&#23384;&#20013;&#30340;&#25968;&#25454;\n$user = Cache::get('user:{$userId}');\nif ($user) {\n    echo \"User name is {$user}\";\n} else {\n    echo \"User not found\";\n}<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#31034;&#20363;&#20013;&#65292;&#25105;&#20204;&#20351;&#29992;<code>Cache::put<\/code>&#26041;&#27861;&#23558;&#29992;&#25143;&#20449;&#24687;&#23384;&#20648;&#21040;&#32531;&#23384;&#20013;&#65292;&#24182;&#35774;&#32622;&#20854;&#26377;&#25928;&#26399;&#20026;60&#31186;&#12290;&#28982;&#21518;&#65292;&#25105;&#20204;&#20351;&#29992;<code>Cache::get<\/code>&#26041;&#27861;&#20174;&#32531;&#23384;&#20013;&#33719;&#21462;&#29992;&#25143;&#20449;&#24687;&#12290;<\/p>\n<h3>3. &#21024;&#38500;&#32531;&#23384;&#25968;&#25454;<\/h3>\n<p>&#22914;&#26524;&#20320;&#24819;&#21024;&#38500;&#32531;&#23384;&#20013;&#30340;&#25968;&#25454;&#65292;&#21487;&#20197;&#20351;&#29992;<code>Cache::forget<\/code>&#26041;&#27861;&#12290;&#20197;&#19979;&#26159;&#19968;&#20010;&#31034;&#20363;&#65306;<\/p>\n<pre><code class=\"language-php\">use IlluminateSupportFacadesCache;\n\n\/\/ &#21024;&#38500;&#32531;&#23384;&#20013;&#30340;&#25968;&#25454;\n$userId = 1;\nCache::forget('user:{$userId}');<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#31034;&#20363;&#20013;&#65292;&#25105;&#20204;&#20351;&#29992;<code>Cache::forget<\/code>&#26041;&#27861;&#21024;&#38500;&#32531;&#23384;&#20013;&#30340;&#29992;&#25143;&#20449;&#24687;&#12290;<\/p>\n<h3>4. &#28165;&#31354;&#32531;&#23384;<\/h3>\n<p>&#22914;&#26524;&#20320;&#38656;&#35201;&#28165;&#31354;&#25972;&#20010;&#32531;&#23384;&#65292;&#21487;&#20197;&#20351;&#29992;<code>Cache::flush<\/code>&#26041;&#27861;&#12290;&#20197;&#19979;&#26159;&#19968;&#20010;&#31034;&#20363;&#65306;<\/p>\n<pre><code class=\"language-php\">use IlluminateSupportFacadesCache;\n\n\/\/ &#28165;&#31354;&#32531;&#23384;\nCache::flush();<\/code><\/pre>\n<p>&#36825;&#20010;&#26041;&#27861;&#20250;&#28165;&#38500;&#25152;&#26377;&#32531;&#23384;&#25968;&#25454;&#12290;<\/p>\n<h3>&#24635;&#32467;<\/h3>\n<p>&#36890;&#36807;&#20197;&#19978;&#27493;&#39588;&#65292;&#20320;&#22312;Laravel&#20013;&#21487;&#20197;&#26041;&#20415;&#22320;&#23384;&#20648;&#21644;&#35835;&#21462;&#32531;&#23384;&#25968;&#25454;&#12290;&#32531;&#23384;&#19981;&#20165;&#21487;&#20197;&#25552;&#39640;&#24212;&#29992;&#30340;&#24615;&#33021;&#65292;&#36824;&#21487;&#20197;&#20943;&#23569;&#25968;&#25454;&#24211;&#26597;&#35810;&#27425;&#25968;&#65292;&#20174;&#32780;&#20248;&#21270;&#25972;&#20307;&#31995;&#32479;&#30340;&#36816;&#34892;&#25928;&#29575;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--2\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/zp-400x300.jpg\" alt=\"&#40657;&#26495;Bug&#35762;&#24072;\" class=\"wp-image-1849\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">&#40657;&#26495;Bug&#35762;&#24072;<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<body><h2>&#20171;&#32461;<\/h2><p>Laravel &#25552;&#20379;&#20102;&#19968;&#31181;&#32479;&#19968;&#30340;&#32531;&#23384;&#31995;&#32479;&#25509;&#21475;&#12290;&#23427;&#33258;&#24102;&#25903;&#25345; Memcached &#21644; Redis &#31561;&#27969;&#34892;&#30340;&#32531;&#23384;&#21518;&#31471;&#12290;&#40664;&#35748;&#24773;&#20917;&#19979;&#65292;Laravel &#20351;&#29992;&#25991;&#20214;&#32531;&#23384;&#39537;&#21160;&#31243;&#24207;&#65292;&#23558;&#32531;&#23384;&#23545;&#35937;&#23384;&#20648;&#22312;&#25991;&#20214;&#31995;&#32479;&#20013;&#12290;&#23545;&#20110;&#22823;&#22411;&#24212;&#29992;&#65292;&#25512;&#33616;&#20351;&#29992;&#20869;&#23384;&#32531;&#23384;&#65292;&#22914; Memcached &#25110; Redis&#65292;&#22240;&#20026;&#23427;&#20204;&#25552;&#20379;&#20102;&#26356;&#22909;&#30340;&#24615;&#33021;&#12290;<\/p><h2>&#35774;&#32622;&#19982;&#37197;&#32622;<\/h2><p>&#31532;&#19968;&#27493;&#26159;&#37197;&#32622;&#24212;&#29992;&#30340;&#32531;&#23384;&#35774;&#32622;&#65292;&#36825;&#21487;&#20197;&#22312;config\/cache.php&#25991;&#20214;&#20013;&#36827;&#34892;&#12290;&#20320;&#21487;&#20197;&#35774;&#32622;&#40664;&#35748;&#30340;&#32531;&#23384;&#39537;&#21160;&#31243;&#24207;&#65292;&#24182;&#20026;&#27599;&#20010;&#39537;&#21160;&#31243;&#24207;&#37197;&#32622;&#20854;&#20351;&#29992;&#30340;&#23384;&#20648;&#24211;&#12290;<\/p><pre><code>'default' =&gt; env('CACHE_DRIVER', 'file'),\n\n'stores' =&gt; [\n    'file' =&gt; [\n        'driver' =&gt; 'file',\n        'path' =&gt; storage_path('framework\/cache\/data'),\n    ],\n    \/\/ More drivers...\n],\n<\/code><\/pre><p>&#35760;&#24471;&#22312; .env &#25991;&#20214;&#20013;&#35774;&#32622;&#27491;&#30830;&#30340; CACHE_DRIVER&#12290;<\/p><h2>&#23384;&#20648;&#39033;&#30446;&#21040;&#32531;&#23384;<\/h2><p>&#20026;&#20102;&#22312;&#32531;&#23384;&#20013;&#23384;&#20648;&#39033;&#30446;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992;&#12290;<code>Cache::put<\/code>&#26041;&#27861;&#12290;&#24744;&#38656;&#35201;&#25351;&#23450;&#23494;&#38053;&#21644;&#20540;&#65292;&#20197;&#21450;&#21487;&#36873;&#30340;&#22312;&#39033;&#30446;&#24212;&#22312;&#22810;&#23569;&#20998;&#38047;&#20869;&#34987;&#31227;&#38500;&#12290;<\/p><pre><code>use IlluminateSupportFacadesCache; \n\n\/\/ Store an item for 10 minutes \nCache::put('key', 'value', 10);<\/code><\/pre><h2>&#27491;&#22312;&#26816;&#26597;&#21830;&#21697;&#26159;&#21542;&#23384;&#22312;&#12290;<\/h2><p>&#35201;&#30830;&#23450;&#19968;&#20010;&#39033;&#30446;&#26159;&#21542;&#22312;&#32531;&#23384;&#20013;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992;&#23427;&#12290;<code>Cache::has<\/code>&#26041;&#27861;&#65292;&#36820;&#22238; true &#25110; false&#65306;<\/p><pre><code>if (Cache::has('key')) { \n   \/\/ The item exists in the cache... \n}<\/code><\/pre><h2>&#20174;&#32531;&#23384;&#35835;&#21462;&#39033;&#30446;<\/h2><p>&#35201;&#20174;&#32531;&#23384;&#20013;&#26816;&#32034;&#39033;&#30446;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992;&#12290;<code>Cache::get<\/code>&#26041;&#27861;&#65306;<\/p><pre><code>$value = Cache::get('key');<\/code><\/pre><p>&#22914;&#26524;&#32531;&#23384;&#20013;&#27809;&#26377;&#35813;&#29289;&#21697;&#30340;&#35805;&#12290;<code>get<\/code>&#36820;&#22238;null&#12290;&#20320;&#21487;&#20197;&#36890;&#36807;&#20256;&#36882;&#31532;&#20108;&#20010;&#21442;&#25968;&#26469;&#25351;&#23450;&#40664;&#35748;&#20540;&#65306;<\/p><pre><code>$value = Cache::get('key', 'default');<\/code><\/pre><h2>&#36882;&#22686;\/&#36882;&#20943;&#20540;<\/h2><p>Laravel &#20063;&#25552;&#20379;&#20102;&#29992;&#20110;&#22312;&#32531;&#23384;&#20013;&#36882;&#22686;&#21644;&#36882;&#20943;&#25972;&#25968;&#20540;&#30340;&#26041;&#27861;&#65306;<\/p><pre><code>Cache::increment('key'); \nCache::decrement('key');<\/code><\/pre><h2>&#26080;&#38480;&#20648;&#23384;&#29289;&#21697;<\/h2><p>&#26377;&#26102;&#65292;&#20320;&#21487;&#33021;&#24076;&#26395;&#23558;&#26576;&#20010;&#39033;&#30446;&#26080;&#38480;&#26399;&#22320;&#23384;&#20648;&#22312;&#32531;&#23384;&#20013;&#65306;<\/p><pre><code>Cache::forever('key', 'value');<\/code><\/pre><h2>&#26816;&#32034;&#19982;&#23384;&#20648;<\/h2><p>&#26377;&#26102;&#65292;&#24744;&#21487;&#33021;&#24076;&#26395;&#20174;&#32531;&#23384;&#20013;&#26816;&#32034;&#39033;&#30446;&#65292;&#20294;&#21516;&#26102;&#23384;&#20648;&#40664;&#35748;&#20540;&#20197;&#34920;&#31034;&#35831;&#27714;&#30340;&#39033;&#30446;&#19981;&#23384;&#22312;&#12290;<\/p><pre><code>$value = Cache::remember('key', $minutes, function () { \n   return DB::table(...)-&gt;get(); \n});<\/code><\/pre><p>&#20320;&#21487;&#20197;&#27704;&#20037;&#23384;&#20648;&#29289;&#21697;&#65306;<\/p><pre><code>$value = Cache::rememberForever('key', function () { \n   return DB::table(...)-&gt;get(); \n});<\/code><\/pre><h2>&#21024;&#38500;&#32531;&#23384;&#20013;&#30340;&#39033;&#30446;<\/h2><p>&#35201;&#20174;&#32531;&#23384;&#20013;&#31227;&#38500;&#39033;&#30446;&#65292;&#35831;&#20351;&#29992;&#12290;<code>Cache::forget<\/code>&#26041;&#27861;&#65306;<\/p><pre><code>Cache::forget('key');<\/code><\/pre><p>&#26368;&#21518;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992;flush&#21629;&#20196;&#28165;&#38500;&#25972;&#20010;&#32531;&#23384;&#12290;<code>Cache::flush<\/code>&#26041;&#27861;&#65292;&#20250;&#31227;&#38500;&#25152;&#26377;&#39033;&#30446;&#65306;<\/p><pre><code>Cache::flush();<\/code><\/pre><h2>&#25968;&#25454;&#24211;&#32531;&#23384;&#38145;<\/h2><p>&#20174;Laravel 5.6&#24320;&#22987;&#65292;&#20320;&#21487;&#20197;&#23545;&#32531;&#23384;&#25805;&#20316;&#33719;&#21462;&#38145;&#65292;&#36825;&#22312;&#38450;&#27490;&#24182;&#21457;&#26465;&#20214;&#65288;race condition&#65289;&#26041;&#38754;&#38750;&#24120;&#26377;&#29992;&#65306;<\/p><pre><code>if (Cache::lock('foo', 10)-&gt;get()) { \n  \/\/ Lock acquired for 10 seconds... \n  Cache::lock('foo')-&gt;release(); \n}<\/code><\/pre><p>&#22312;&#36825;&#31181;&#24773;&#20917;&#19979;&#65292;&#31532;&#20108;&#20010;&#21442;&#25968;&#20026;&hellip;&hellip;<code>get<\/code>&#26041;&#27861;&#26159;&#22312;&#38145;&#23450;&#26102;&#24212;&#20445;&#25345;&#38145;&#20303;&#22810;&#38271;&#26102;&#38388;&#12290;<\/p><h2>&#26631;&#31614;&#21644;&#24050;&#32531;&#23384;&#30340;&#26631;&#31614;<\/h2><p>&#23545;&#20110;&#21830;&#24215;&#26469;&#35828;&#65292;&#25903;&#25345;&#26631;&#31614;&#21151;&#33021;&#65292;&#20320;&#21487;&#20197;&#20808;&#22312;&#32531;&#23384;&#20013;&#26631;&#35760;&#30456;&#20851;&#30340;&#39033;&#30446;&#65292;&#28982;&#21518;&#28165;&#38500;&#25152;&#26377;&#24050;&#20998;&#37197;&#32473;&#35813;&#26631;&#31614;&#30340;&#32531;&#23384;&#12290;&#36825;&#31181;&#26041;&#27861;&#29305;&#21035;&#36866;&#29992;&#20110;&#23545;&#30456;&#20851;&#39033;&#30446;&#36827;&#34892;&#20998;&#31867;&#12290;<\/p><pre><code>Cache::tags(['people', 'artists'])-&gt;put('John', $john, $minutes); Cache::tags(['people'])-&gt;flush();<\/code><\/pre><h2>&#39640;&#32423;&#31034;&#20363;&#65306;&#20179;&#24211;&#27169;&#24335;<\/h2><p>&#22312;Laravel&#20013;&#23454;&#29616;Repository&#27169;&#24335;&#26159;&#19968;&#31181;&#24456;&#22909;&#30340;&#26041;&#24335;&#65292;&#21487;&#20197;&#25277;&#35937;&#32531;&#23384;&#23618;&#21644;&#20854;&#20182;&#25968;&#25454;&#35775;&#38382;&#36923;&#36753;&#12290;&#36825;&#31181;&#27169;&#24335;&#28041;&#21450;&#21019;&#24314;&#20179;&#24211;&#31867;&#26469;&#38598;&#20013;&#22788;&#29702;&#20174;&#25968;&#25454;&#28304;&#65288;&#22914;&#25968;&#25454;&#24211;&#65289;&#33719;&#21462;&#25968;&#25454;&#30340;&#36923;&#36753;&#65292;&#24182;&#19988;&#29305;&#21035;&#36866;&#29992;&#20110;&#32531;&#23384;&#12290;&#20197;&#19979;&#26159;&#19968;&#20010;&#22914;&#20309;&#23454;&#29616;&#36825;&#20010;&#27169;&#24335;&#30340;&#20363;&#23376;&#65306;<\/p><h3>&#27493;&#39588;1&#65306;&#21019;&#24314;Repository&#25509;&#21475;<\/h3><p>&#39318;&#20808;&#65292;&#20026;&#20320;&#30340;&#20179;&#24211;&#23450;&#20041;&#19968;&#20010;&#25509;&#21475;&#12290;&#36825;&#23558;&#30830;&#20445;&#20219;&#20309;&#20855;&#20307;&#30340;&#20179;&#24211;&#31867;&#37117;&#23454;&#29616;&#29305;&#23450;&#30340;&#26041;&#27861;&#12290;<\/p><pre><code>namespace AppRepositories;\n\ninterface ProductRepositoryInterface\n{\n    public function getAll();\n\n    public function findById($id);\n\n    \/\/ Other necessary methods...\n}\n<\/code><\/pre><h3>&#27493;&#39588; 2&#65306;&#21019;&#24314;&#23454;&#20307;&#20179;&#24211;<\/h3><p>&#25509;&#19979;&#26469;&#65292;&#21019;&#24314;&#19968;&#20010;&#23454;&#29616;&#35813;&#25509;&#21475;&#30340;&#20855;&#20307;&#31867;&#12290;&#36825;&#26159;&#23454;&#29616;&#32531;&#23384;&#36923;&#36753;&#30340;&#22320;&#26041;&#12290;<\/p><pre><code>namespace AppRepositories;\n\nuse AppModelsProduct;\nuse IlluminateSupportFacadesCache;\n\nclass ProductRepository implements ProductRepositoryInterface\n{\n    protected $model;\n\n    public function __construct(Product $product)\n    {\n        $this-&gt;model = $product;\n    }\n\n    public function getAll()\n    {\n        $cacheKey = 'products_all';\n        return Cache::remember($cacheKey, 3600, function () {\n            return $this-&gt;model-&gt;all();\n        });\n    }\n\n    public function findById($id)\n    {\n        $cacheKey = 'product_'.$id;\n        return Cache::remember($cacheKey, 3600, function () use ($id) {\n            return $this-&gt;model-&gt;find($id);\n        });\n    }\n\n    \/\/ Implement other methods...\n}\n<\/code><\/pre><h3>&#27493;&#39588; 3&#65306;&#32465;&#23450;&#25509;&#21475;&#21040;&#23454;&#29616;<\/h3><p>&#22312;&#26381;&#21153;&#25552;&#20379;&#21830;&#65288;&#22914;<code>AppServiceProvider<\/code>)&#65292;&#32465;&#23450;&#25509;&#21475;&#21040;&#20855;&#20307;&#30340;&#31867;&#12290;<\/p><pre><code>namespace AppProviders;\n\nuse IlluminateSupportServiceProvider;\nuse AppRepositoriesProductRepositoryInterface;\nuse AppRepositoriesProductRepository;\n\nclass AppServiceProvider extends ServiceProvider\n{\n    public function register()\n    {\n        $this-&gt;app-&gt;bind(ProductRepositoryInterface::class, ProductRepository::class);\n    }\n\n    \/\/ ...\n}\n<\/code><\/pre><h3>&#27493;&#39588;4&#65306;&#22312;&#25511;&#21046;&#22120;&#20013;&#20351;&#29992;&#20179;&#24211;<\/h3><p>&#26368;&#21518;&#65292;&#36890;&#36807;&#22312;&#26500;&#36896;&#20989;&#25968;&#20013;&#31867;&#22411;&#25552;&#31034;&#25509;&#21475;&#26469;&#22312;&#25511;&#21046;&#22120;&#20013;&#20351;&#29992;&#20179;&#24211;&#12290;<\/p><pre><code>namespace AppHttpControllers;\n\nuse AppRepositoriesProductRepositoryInterface;\n\nclass ProductController extends Controller\n{\n    protected $productRepository;\n\n    public function __construct(ProductRepositoryInterface $productRepository)\n    {\n        $this-&gt;productRepository = $productRepository;\n    }\n\n    public function index()\n    {\n        $products = $this-&gt;productRepository-&gt;getAll();\n        return view('products.index', compact('products'));\n    }\n\n    \/\/ Other methods...\n}\n<\/code><\/pre><h3>&#35299;&#37322;<\/h3><p>&#25511;&#21046;&#22120;&#65306;&#20351;&#29992;&#20102;&#25509;&#21475;&#27169;&#24335;&#65292;&#36825;&#20351;&#24471;&#25105;&#20204;&#21487;&#20197;&#36731;&#26494;&#22320;&#20999;&#25442;&#23454;&#29616;&#26041;&#24335;&#25110;&#22312;&#27979;&#35797;&#20013;mock&#25481;&#20179;&#24211;&#12290;<\/p><p>&#26381;&#21153;&#25552;&#20379;&#32773;&#65306;&#32465;&#23450;&#25509;&#21475;&#21040;&#20855;&#20307;&#30340;&#23454;&#29616;&#65292;&#20801;&#35768;Laravel&#30340;&#20381;&#36182;&#27880;&#20837;&#26469;&#27491;&#30830;&#35299;&#26512;&#23454;&#20363;&#12290;<\/p><p>&#28151;&#20957;&#22303;&#20179;&#24211;&#65306;&#23454;&#29616;&#20102;&#25509;&#21475;&#65292;&#23553;&#35013;&#20102;&#25968;&#25454;&#35775;&#38382;&#36923;&#36753;&#21644;&#32531;&#23384;&#12290;<\/p><p>&#20179;&#24211;&#25509;&#21475;&#65306;&#23450;&#20041;&#20102;&#20179;&#24211;&#30340;&#21512;&#21516;&#65292;&#30830;&#20445;&#19968;&#33268;&#24615;&#19982;&#21487;&#39044;&#27979;&#24615;&#12290;<\/p><p>&#36825;&#20010;&#27169;&#24335;&#26377;&#21161;&#20110;&#20445;&#25345;&#25511;&#21046;&#22120;&#30246;&#65292;&#36981;&#23432;&#21333;&#19968;&#32844;&#36131;&#21407;&#21017;&#12290;&#23427;&#36824;&#33021;&#20351;&#24744;&#30340;&#24212;&#29992;&#31243;&#24207;&#26356;&#26131;&#20110;&#32500;&#25252;&#21644;&#27979;&#35797;&#12290;<\/p><h2>&#32467;&#35770;&#12290;<\/h2><p>&#32531;&#23384;&#22312;&#20248;&#21270;Laravel&#24212;&#29992;&#31243;&#24207;&#24615;&#33021;&#26041;&#38754;&#33267;&#20851;&#37325;&#35201;&#12290;&#36890;&#36807;&#26377;&#25928;&#22320;&#21033;&#29992;Laravel&#30340;&#32531;&#23384;&#21151;&#33021;&#65292;&#24744;&#21487;&#20197;&#26174;&#33879;&#20943;&#23569;&#25968;&#25454;&#24211;&#36127;&#36733;&#24182;&#25552;&#39640;&#24212;&#29992;&#21709;&#24212;&#36895;&#24230;&#12290;<\/p><\/body>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--3\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/xygcfznnzczhsdmwydzhsmzzzmddnq-400x300.jpg\" alt=\"&#38271;&#24037;&#30721;&#22900;1523\" class=\"wp-image-2906\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">&#38271;&#24037;&#30721;&#22900;1523<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#22312; Laravel &#20013;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992; Eloquent ORM &#25110;&#32773; Redis &#20316;&#20026;&#32531;&#23384;&#35299;&#20915;&#26041;&#26696;&#12290;&#19979;&#38754;&#25105;&#23558;&#20171;&#32461;&#22914;&#20309;&#20351;&#29992; Eloquent ORM &#23384;&#20648;&#21644;&#35835;&#21462;&#32531;&#23384;&#25968;&#25454;&#12290;<\/p>\n<h3>&#20351;&#29992; Eloquent ORM &#23384;&#20648;&#32531;&#23384;<\/h3>\n<p>&#39318;&#20808;&#65292;&#30830;&#20445;&#20320;&#30340;&#39033;&#30446;&#24050;&#23433;&#35013;&#20102; Eloquent ORM &#21644; Redis&#12290;<\/p>\n<ol>\n<li>&#39318;&#20808;&#65292;&#21019;&#24314;&#19968;&#20010;&#26032;&#30340;&#32531;&#23384;&#25991;&#20214;&#22841;&#26469;&#23384;&#25918;&#20320;&#30340;&#32531;&#23384;&#25968;&#25454;&#12290;<\/li>\n<li>&#22312;&#20320;&#30340;&#25511;&#21046;&#22120;&#26041;&#27861;&#20013;&#65292;&#20351;&#29992; <code>Cache::put<\/code> &#26041;&#27861;&#23384;&#20648;&#32531;&#23384;&#25968;&#25454;&#65306;<\/li>\n<\/ol>\n<pre><code class=\"language-php\">public function storeInCache($key, $value)\n{\n    Cache::put($key, $value, now()-&gt;addDays(30)); \/\/ &#35774;&#32622;&#32531;&#23384;&#26377;&#25928;&#26399;&#20026;&#19968;&#20010;&#26376;\n}<\/code><\/pre>\n<p>&#36825;&#34892;&#20195;&#30721;&#30340;&#24847;&#24605;&#26159;&#65306;&#25226; <code>$value<\/code> &#30340;&#20540;&#25918;&#20837;&#21517;&#20026; <code>$key<\/code> &#30340;&#32531;&#23384;&#65292;&#24182;&#35774;&#32622;&#26377;&#25928;&#26399;&#20026;&#19968;&#20010;&#26376;&#65288;&#21363; 30 &#22825;&#65289;&#12290;<\/p>\n<h3>&#35835;&#21462;&#32531;&#23384;&#25968;&#25454;<\/h3>\n<p>&#35201;&#20174;&#32531;&#23384;&#20013;&#33719;&#21462;&#25968;&#25454;&#65292;&#35831;&#35843;&#29992; <code>Cache::get<\/code> &#26041;&#27861;&#24182;&#25552;&#20379;&#32531;&#23384;&#38190;&#20316;&#20026;&#21442;&#25968;&#12290;<\/p>\n<pre><code class=\"language-php\">$cacheData = Cache::get('your_cache_key');<\/code><\/pre>\n<p>&#22914;&#26524;&#32531;&#23384;&#38190;&#19981;&#23384;&#22312;&#65292;&#21017;&#36820;&#22238; <code>null<\/code>&#12290;<\/p>\n<h3>&#31034;&#20363;&#20195;&#30721;<\/h3>\n<p>&#20551;&#35774;&#25105;&#20204;&#26377;&#19968;&#20010;&#21517;&#20026; <code>users<\/code> &#30340;&#34920;&#65292;&#20854;&#20013;&#21253;&#21547;&#19968;&#20010;&#21517;&#20026; <code>name<\/code> &#30340;&#23383;&#27573;&#65292;&#25105;&#20204;&#21487;&#20197;&#36825;&#26679;&#23384;&#20648;&#21644;&#35835;&#21462;&#32531;&#23384;&#65306;<\/p>\n<pre><code class=\"language-php\">\/\/ &#23384;&#20648;&#32531;&#23384;&#25968;&#25454;\npublic function storeUserInCache($userId, $name)\n{\n    $this-&gt;storeInCache('user_'.$userId.'_name', $name);\n}\n\n\/\/ &#20174;&#32531;&#23384;&#20013;&#33719;&#21462;&#29992;&#25143;&#20449;&#24687;\npublic function getUserFromCache($userId)\n{\n    return $this-&gt;getUserByUserId($userId)-&gt;first();\n}<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;&#25105;&#20204;&#22312;&#23384;&#20648;&#26102;&#20256;&#20837;&#20102;&#29992;&#25143;&#30340; ID &#21644;&#22995;&#21517;&#65292;&#28982;&#21518;&#22312;&#35835;&#21462;&#26102;&#36890;&#36807; ID &#33719;&#21462;&#29992;&#25143;&#23454;&#20363;&#24182;&#26816;&#26597;&#26159;&#21542;&#23384;&#22312;&#12290;<\/p>\n<p>&#35831;&#27880;&#24847;&#65292;&#20197;&#19978;&#20195;&#30721;&#31034;&#20363;&#20013;&#30340;&#32531;&#23384;&#25805;&#20316;&#20165;&#29992;&#20110;&#28436;&#31034;&#30446;&#30340;&#12290;&#23454;&#38469;&#24212;&#29992;&#20013;&#65292;&#20320;&#38656;&#35201;&#26681;&#25454;&#20855;&#20307;&#38656;&#27714;&#35843;&#25972;&#32531;&#23384;&#37197;&#32622;&#21644;&#31574;&#30053;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bug&#32534;&#35793;&#29422; &#22312;Laravel&#20013;&#65292;&#32531;&#23384;&#26159;&#19968;&#31181;&#38750;&#24120;&#24378;&#22823;&#30340;&#24037;&#20855;&#038;#652..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[44],"tuisongtax":[],"class_list":["post-847","my1js","type-my1js","status-publish","hentry","my1js2nav-laravel"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/847","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=847"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=847"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=847"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}