2
2
3
3
namespace Opcodes \LogViewer ;
4
4
5
- use Carbon \CarbonInterface ;
6
5
use Illuminate \Support \Arr ;
7
- use Illuminate \Support \Facades \Cache ;
8
6
use Illuminate \Support \Str ;
9
7
use Opcodes \LogViewer \Events \LogFileDeleted ;
10
8
use Opcodes \LogViewer \Exceptions \InvalidRegularExpression ;
11
- use Opcodes \LogViewer \Facades \LogViewer ;
12
- use Opcodes \LogViewer \Utils \GenerateCacheKey ;
13
9
use Opcodes \LogViewer \Utils \Utils ;
14
10
use Symfony \Component \HttpFoundation \BinaryFileResponse ;
15
11
16
12
class LogFile
17
13
{
14
+ use Concerns \LogFile \HasMetadata;
15
+ use Concerns \LogFile \CanCacheData;
16
+
18
17
public string $ path ;
19
18
public string $ name ;
20
19
public string $ identifier ;
21
20
public string $ subFolder = '' ;
22
- protected array $ metaData ;
23
21
private array $ _logIndexCache ;
24
22
25
23
public function __construct (string $ path )
@@ -32,7 +30,7 @@ public function __construct(string $path)
32
30
$ this ->subFolder = str_replace ($ this ->name , '' , $ path );
33
31
$ this ->subFolder = rtrim ($ this ->subFolder , DIRECTORY_SEPARATOR );
34
32
35
- $ this ->metaData = Cache:: get ( $ this -> metaDataCacheKey (), [] );
33
+ $ this ->loadMetadata ( );
36
34
}
37
35
38
36
public function index (string $ query = null ): LogIndex
@@ -81,78 +79,20 @@ public function download(): BinaryFileResponse
81
79
return response ()->download ($ this ->path );
82
80
}
83
81
84
- protected function cacheTtl (): CarbonInterface
85
- {
86
- return now ()->addWeek ();
87
- }
88
-
89
- protected function cacheKey (): string
90
- {
91
- return GenerateCacheKey::for ($ this );
92
- }
93
-
94
82
public function addRelatedIndex (LogIndex $ logIndex ): void
95
83
{
96
- $ relatedIndices = collect ($ this ->getMetaData ('related_indices ' , []));
84
+ $ relatedIndices = collect ($ this ->getMetadata ('related_indices ' , []));
97
85
$ relatedIndices [$ logIndex ->identifier ] = Arr::only (
98
86
$ logIndex ->getMetadata (),
99
87
['query ' , 'last_scanned_file_position ' ]
100
88
);
101
89
102
- $ this ->setMetaData ('related_indices ' , $ relatedIndices ->toArray ());
103
- }
104
-
105
- protected function relatedCacheKeysKey (): string
106
- {
107
- return GenerateCacheKey::for ($ this , 'related-cache-keys ' );
108
- }
109
-
110
- public function addRelatedCacheKey (string $ key ): void
111
- {
112
- $ keys = $ this ->getRelatedCacheKeys ();
113
- $ keys [] = $ key ;
114
- Cache::put (
115
- $ this ->relatedCacheKeysKey (),
116
- array_unique ($ keys ),
117
- $ this ->cacheTtl ()
118
- );
119
- }
120
-
121
- protected function getRelatedCacheKeys (): array
122
- {
123
- return array_merge (
124
- Cache::get ($ this ->relatedCacheKeysKey (), []),
125
- [
126
- $ this ->indexCacheKeyForQuery (),
127
- $ this ->indexCacheKeyForQuery ().':last-scan ' ,
128
- ]
129
- );
130
- }
131
-
132
- protected function indexCacheKeyForQuery (string $ query = '' ): string
133
- {
134
- return GenerateCacheKey::for ($ this , md5 ($ query ) . ':index ' );
135
- }
136
-
137
- public function clearCache (): void
138
- {
139
- foreach ($ this ->getMetaData ('related_indices ' , []) as $ indexIdentifier => $ indexMetadata ) {
140
- $ this ->index ($ indexMetadata ['query ' ])->clearCache ();
141
- }
142
-
143
- foreach ($ this ->getRelatedCacheKeys () as $ relatedCacheKey ) {
144
- Cache::forget ($ relatedCacheKey );
145
- }
146
-
147
- Cache::forget ($ this ->metaDataCacheKey ());
148
- Cache::forget ($ this ->relatedCacheKeysKey ());
149
-
150
- $ this ->index ()->clearCache ();
90
+ $ this ->setMetadata ('related_indices ' , $ relatedIndices ->toArray ());
151
91
}
152
92
153
93
public function getLastScannedFilePositionForQuery (?string $ query = '' ): ?int
154
94
{
155
- foreach ($ this ->getMetaData ('related_indices ' , []) as $ indexIdentifier => $ indexMetadata ) {
95
+ foreach ($ this ->getMetadata ('related_indices ' , []) as $ indexIdentifier => $ indexMetadata ) {
156
96
if ($ query === $ indexMetadata ['query ' ]) {
157
97
return $ indexMetadata ['last_scanned_file_position ' ] ?? 0 ;
158
98
}
@@ -161,43 +101,15 @@ public function getLastScannedFilePositionForQuery(?string $query = ''): ?int
161
101
return null ;
162
102
}
163
103
164
- protected function metaDataCacheKey (): string
165
- {
166
- return GenerateCacheKey::for ($ this , 'metadata ' );
167
- }
168
-
169
- public function setMetaData (string $ attribute , $ value ): void
170
- {
171
- $ this ->metaData [$ attribute ] = $ value ;
172
- }
173
-
174
- public function getMetaData (string $ attribute = null , $ default = null ): mixed
175
- {
176
- if (! isset ($ this ->metaData )) {
177
- $ this ->metaData = Cache::get ($ this ->metaDataCacheKey (), []);
178
- }
179
-
180
- if (isset ($ attribute )) {
181
- return $ this ->metaData [$ attribute ] ?? $ default ;
182
- }
183
-
184
- return $ this ->metaData ;
185
- }
186
-
187
- public function saveMetaData (): void
188
- {
189
- Cache::put ($ this ->metaDataCacheKey (), $ this ->metaData , $ this ->cacheTtl ());
190
- }
191
-
192
104
public function earliestTimestamp (): int
193
105
{
194
- return $ this ->getMetaData ('earliest_timestamp ' )
106
+ return $ this ->getMetadata ('earliest_timestamp ' )
195
107
?? (is_file ($ this ->path ) ? filemtime ($ this ->path ) : 0 );
196
108
}
197
109
198
110
public function latestTimestamp (): int
199
111
{
200
- return $ this ->getMetaData ('latest_timestamp ' )
112
+ return $ this ->getMetadata ('latest_timestamp ' )
201
113
?? (is_file ($ this ->path ) ? filemtime ($ this ->path ) : 0 );
202
114
}
203
115
0 commit comments