Skip to content

Commit 15a326e

Browse files
committed
缓存首页提升首次打开速度
1 parent c589b5b commit 15a326e

File tree

5 files changed

+243
-3
lines changed

5 files changed

+243
-3
lines changed

app/Http/Kernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,6 @@ class Kernel extends HttpKernel
6767
'signed' => \App\Http\Middleware\ValidateSignature::class,
6868
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
6969
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
70+
'cache.response' => \Spatie\ResponseCache\Middlewares\CacheResponse::class,
7071
];
7172
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"laravel/tinker": "^2.8",
1818
"openai-php/client": "^0.3.4",
1919
"orhanerday/open-ai": "^4.7",
20+
"spatie/laravel-responsecache": "^7.4",
2021
"spiral/roadrunner": "^2.8.2",
2122
"tightenco/ziggy": "^1.0"
2223
},
@@ -74,4 +75,4 @@
7475
},
7576
"minimum-stability": "stable",
7677
"prefer-stable": true
77-
}
78+
}

composer.lock

Lines changed: 145 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/responsecache.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
return [
4+
/*
5+
* Determine if the response cache middleware should be enabled.
6+
*/
7+
'enabled' => env('RESPONSE_CACHE_ENABLED', true),
8+
9+
/*
10+
* The given class will determinate if a request should be cached. The
11+
* default class will cache all successful GET-requests.
12+
*
13+
* You can provide your own class given that it implements the
14+
* CacheProfile interface.
15+
*/
16+
'cache_profile' => Spatie\ResponseCache\CacheProfiles\CacheAllSuccessfulGetRequests::class,
17+
18+
/*
19+
* Optionally, you can specify a header that will force a cache bypass.
20+
* This can be useful to monitor the performance of your application.
21+
*/
22+
'cache_bypass_header' => [
23+
'name' => env('CACHE_BYPASS_HEADER_NAME', null),
24+
'value' => env('CACHE_BYPASS_HEADER_VALUE', null),
25+
],
26+
27+
/*
28+
* When using the default CacheRequestFilter this setting controls the
29+
* default number of seconds responses must be cached.
30+
*/
31+
'cache_lifetime_in_seconds' => env('RESPONSE_CACHE_LIFETIME', 60 * 60 * 24 * 7),
32+
33+
/*
34+
* This setting determines if a http header named with the cache time
35+
* should be added to a cached response. This can be handy when
36+
* debugging.
37+
*/
38+
'add_cache_time_header' => env('APP_DEBUG', true),
39+
40+
/*
41+
* This setting determines the name of the http header that contains
42+
* the time at which the response was cached
43+
*/
44+
'cache_time_header_name' => env('RESPONSE_CACHE_HEADER_NAME', 'laravel-responsecache'),
45+
46+
/*
47+
* This setting determines if a http header named with the cache age
48+
* should be added to a cached response. This can be handy when
49+
* debugging.
50+
* ONLY works when "add_cache_time_header" is also active!
51+
*/
52+
'add_cache_age_header' => env('RESPONSE_CACHE_AGE_HEADER', false),
53+
54+
/*
55+
* This setting determines the name of the http header that contains
56+
* the age of cache
57+
*/
58+
'cache_age_header_name' => env('RESPONSE_CACHE_AGE_HEADER_NAME', 'laravel-responsecache-age'),
59+
60+
/*
61+
* Here you may define the cache store that should be used to store
62+
* requests. This can be the name of any store that is
63+
* configured in app/config/cache.php
64+
*/
65+
'cache_store' => env('RESPONSE_CACHE_DRIVER', 'file'),
66+
67+
/*
68+
* Here you may define replacers that dynamically replace content from the response.
69+
* Each replacer must implement the Replacer interface.
70+
*/
71+
'replacers' => [
72+
\Spatie\ResponseCache\Replacers\CsrfTokenReplacer::class,
73+
],
74+
75+
/*
76+
* If the cache driver you configured supports tags, you may specify a tag name
77+
* here. All responses will be tagged. When clearing the responsecache only
78+
* items with that tag will be flushed.
79+
*
80+
* You may use a string or an array here.
81+
*/
82+
'cache_tag' => '',
83+
84+
/*
85+
* This class is responsible for generating a hash for a request. This hash
86+
* is used to look up an cached response.
87+
*/
88+
'hasher' => \Spatie\ResponseCache\Hasher\DefaultHasher::class,
89+
90+
/*
91+
* This class is responsible for serializing responses.
92+
*/
93+
'serializer' => \Spatie\ResponseCache\Serializers\DefaultSerializer::class,
94+
];

routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
|
1616
*/
1717

18-
Route::get('/', ChatController::class . '@index')->name('home');
18+
Route::get('/', ChatController::class . '@index')->name('home')->middleware('cache.response');
1919
Route::get('/messages', ChatController::class . '@messages')->name('messages');
2020
Route::post('/chat', ChatController::class . '@chat')->name('chat')->middleware('throttle:chat');
2121
Route::get('/stream', ChatController::class . '@stream')->name('stream');

0 commit comments

Comments
 (0)