Skip to content

Commit 3adaffb

Browse files
committed
refactor LogFile parts into traits; add Log Viewer Cache facade
1 parent 285cbcc commit 3adaffb

16 files changed

+176
-137
lines changed

src/Concerns/LogFile/CanCacheData.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Opcodes\LogViewer\Concerns\LogFile;
4+
5+
use Carbon\CarbonInterface;
6+
use Illuminate\Contracts\Cache\Repository;
7+
use Opcodes\LogViewer\Facades\Cache;
8+
use Opcodes\LogViewer\Utils\GenerateCacheKey;
9+
10+
trait CanCacheData
11+
{
12+
protected function indexCacheKeyForQuery(string $query = ''): string
13+
{
14+
return GenerateCacheKey::for($this, md5($query) . ':index');
15+
}
16+
17+
public function clearCache(): void
18+
{
19+
foreach ($this->getMetadata('related_indices', []) as $indexIdentifier => $indexMetadata) {
20+
$this->index($indexMetadata['query'])->clearCache();
21+
}
22+
23+
foreach ($this->getRelatedCacheKeys() as $relatedCacheKey) {
24+
Cache::forget($relatedCacheKey);
25+
}
26+
27+
Cache::forget($this->metadataCacheKey());
28+
Cache::forget($this->relatedCacheKeysKey());
29+
30+
$this->index()->clearCache();
31+
}
32+
33+
protected function cacheTtl(): CarbonInterface
34+
{
35+
return now()->addWeek();
36+
}
37+
38+
protected function cacheKey(): string
39+
{
40+
return GenerateCacheKey::for($this);
41+
}
42+
43+
protected function relatedCacheKeysKey(): string
44+
{
45+
return GenerateCacheKey::for($this, 'related-cache-keys');
46+
}
47+
48+
public function addRelatedCacheKey(string $key): void
49+
{
50+
$keys = $this->getRelatedCacheKeys();
51+
$keys[] = $key;
52+
Cache::put(
53+
$this->relatedCacheKeysKey(),
54+
array_unique($keys),
55+
$this->cacheTtl()
56+
);
57+
}
58+
59+
protected function getRelatedCacheKeys(): array
60+
{
61+
return array_merge(
62+
Cache::get($this->relatedCacheKeysKey(), []),
63+
[
64+
$this->indexCacheKeyForQuery(),
65+
$this->indexCacheKeyForQuery().':last-scan',
66+
]
67+
);
68+
}
69+
70+
protected function metadataCacheKey(): string
71+
{
72+
return GenerateCacheKey::for($this, 'metadata');
73+
}
74+
75+
protected function loadMetadataFromCache(): array
76+
{
77+
return Cache::get($this->metadataCacheKey(), []);
78+
}
79+
80+
protected function saveMetadataToCache(array $data): void
81+
{
82+
Cache::put($this->metadataCacheKey(), $data, $this->cacheTtl());
83+
}
84+
}

src/Concerns/LogFile/HasMetadata.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Opcodes\LogViewer\Concerns\LogFile;
4+
5+
trait HasMetadata
6+
{
7+
protected array $metadata;
8+
9+
public function setMetadata(string $attribute, $value): void
10+
{
11+
$this->metadata[$attribute] = $value;
12+
}
13+
14+
public function getMetadata(string $attribute = null, $default = null): mixed
15+
{
16+
if (! isset($this->metadata)) {
17+
$this->loadMetadata();
18+
}
19+
20+
if (isset($attribute)) {
21+
return $this->metadata[$attribute] ?? $default;
22+
}
23+
24+
return $this->metadata;
25+
}
26+
27+
public function saveMetadata(): void
28+
{
29+
$this->saveMetadataToCache($this->metadata);
30+
}
31+
32+
protected function loadMetadata(): void
33+
{
34+
$this->metadata = $this->loadMetadataFromCache();
35+
}
36+
}

src/Concerns/LogIndex/CanCacheIndex.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
namespace Opcodes\LogViewer\Concerns\LogIndex;
44

55
use Carbon\CarbonInterface;
6-
use Illuminate\Contracts\Cache\Repository;
7-
use Illuminate\Support\Facades\Cache;
6+
use Opcodes\LogViewer\Facades\Cache;
87
use Opcodes\LogViewer\LogIndexChunk;
98
use Opcodes\LogViewer\Utils\GenerateCacheKey;
109

@@ -14,26 +13,26 @@ public function clearCache(): void
1413
{
1514
$this->clearChunksFromCache();
1615

17-
$this->cache()->forget($this->metaCacheKey());
18-
$this->cache()->forget($this->cacheKey());
16+
Cache::forget($this->metaCacheKey());
17+
Cache::forget($this->cacheKey());
1918

2019
// this will reset all properties to default, because it won't find any cached settings for this index
2120
$this->loadMetadata();
2221
}
2322

2423
protected function saveMetadataToCache(): void
2524
{
26-
$this->cache()->put($this->metaCacheKey(), $this->getMetadata(), $this->cacheTtl());
25+
Cache::put($this->metaCacheKey(), $this->getMetadata(), $this->cacheTtl());
2726
}
2827

2928
protected function getMetadataFromCache(): array
3029
{
31-
return $this->cache()->get($this->metaCacheKey(), []);
30+
return Cache::get($this->metaCacheKey(), []);
3231
}
3332

3433
protected function saveChunkToCache(LogIndexChunk $chunk): void
3534
{
36-
$this->cache()->put(
35+
Cache::put(
3736
$this->chunkCacheKey($chunk->index),
3837
$chunk->data,
3938
$this->cacheTtl()
@@ -42,13 +41,13 @@ protected function saveChunkToCache(LogIndexChunk $chunk): void
4241

4342
protected function getChunkDataFromCache(int $index, $default = null): ?array
4443
{
45-
return $this->cache()->get($this->chunkCacheKey($index), $default);
44+
return Cache::get($this->chunkCacheKey($index), $default);
4645
}
4746

4847
protected function clearChunksFromCache(): void
4948
{
5049
foreach ($this->getChunkDefinitions() as $chunkDefinition) {
51-
$this->cache()->forget($this->chunkCacheKey($chunkDefinition['index']));
50+
Cache::forget($this->chunkCacheKey($chunkDefinition['index']));
5251
}
5352
}
5453

@@ -67,11 +66,6 @@ protected function chunkCacheKey(int $index): string
6766
return GenerateCacheKey::for($this, "chunk:$index");
6867
}
6968

70-
protected function cache(): Repository
71-
{
72-
return app('log-viewer-cache');
73-
}
74-
7569
protected function cacheTtl(): CarbonInterface
7670
{
7771
if (! empty($this->query)) {

src/Concerns/LogIndex/CanSplitIndexIntoChunks.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace Opcodes\LogViewer\Concerns\LogIndex;
44

5-
use Illuminate\Support\Facades\Cache;
65
use Opcodes\LogViewer\Exceptions\InvalidChunkSizeException;
76
use Opcodes\LogViewer\LogIndexChunk;
8-
use Opcodes\LogViewer\Utils\GenerateCacheKey;
97

108
trait CanSplitIndexIntoChunks
119
{

src/Concerns/LogIndex/HasMetadata.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Opcodes\LogViewer\Concerns\LogIndex;
44

5-
use Illuminate\Support\Facades\Cache;
6-
75
trait HasMetadata
86
{
97
public function getMetadata(): array

src/Facades/Cache.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Opcodes\LogViewer\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* @mixin \Illuminate\Contracts\Cache\Repository
9+
*
10+
* @see \Illuminate\Cache\Repository
11+
*/
12+
class Cache extends Facade
13+
{
14+
protected static function getFacadeAccessor()
15+
{
16+
return 'log-viewer-cache';
17+
}
18+
}

src/LogFile.php

Lines changed: 9 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@
22

33
namespace Opcodes\LogViewer;
44

5-
use Carbon\CarbonInterface;
65
use Illuminate\Support\Arr;
7-
use Illuminate\Support\Facades\Cache;
86
use Illuminate\Support\Str;
97
use Opcodes\LogViewer\Events\LogFileDeleted;
108
use Opcodes\LogViewer\Exceptions\InvalidRegularExpression;
11-
use Opcodes\LogViewer\Facades\LogViewer;
12-
use Opcodes\LogViewer\Utils\GenerateCacheKey;
139
use Opcodes\LogViewer\Utils\Utils;
1410
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1511

1612
class LogFile
1713
{
14+
use Concerns\LogFile\HasMetadata;
15+
use Concerns\LogFile\CanCacheData;
16+
1817
public string $path;
1918
public string $name;
2019
public string $identifier;
2120
public string $subFolder = '';
22-
protected array $metaData;
2321
private array $_logIndexCache;
2422

2523
public function __construct(string $path)
@@ -32,7 +30,7 @@ public function __construct(string $path)
3230
$this->subFolder = str_replace($this->name, '', $path);
3331
$this->subFolder = rtrim($this->subFolder, DIRECTORY_SEPARATOR);
3432

35-
$this->metaData = Cache::get($this->metaDataCacheKey(), []);
33+
$this->loadMetadata();
3634
}
3735

3836
public function index(string $query = null): LogIndex
@@ -81,78 +79,20 @@ public function download(): BinaryFileResponse
8179
return response()->download($this->path);
8280
}
8381

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-
9482
public function addRelatedIndex(LogIndex $logIndex): void
9583
{
96-
$relatedIndices = collect($this->getMetaData('related_indices', []));
84+
$relatedIndices = collect($this->getMetadata('related_indices', []));
9785
$relatedIndices[$logIndex->identifier] = Arr::only(
9886
$logIndex->getMetadata(),
9987
['query', 'last_scanned_file_position']
10088
);
10189

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());
15191
}
15292

15393
public function getLastScannedFilePositionForQuery(?string $query = ''): ?int
15494
{
155-
foreach ($this->getMetaData('related_indices', []) as $indexIdentifier => $indexMetadata) {
95+
foreach ($this->getMetadata('related_indices', []) as $indexIdentifier => $indexMetadata) {
15696
if ($query === $indexMetadata['query']) {
15797
return $indexMetadata['last_scanned_file_position'] ?? 0;
15898
}
@@ -161,43 +101,15 @@ public function getLastScannedFilePositionForQuery(?string $query = ''): ?int
161101
return null;
162102
}
163103

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-
192104
public function earliestTimestamp(): int
193105
{
194-
return $this->getMetaData('earliest_timestamp')
106+
return $this->getMetadata('earliest_timestamp')
195107
?? (is_file($this->path) ? filemtime($this->path) : 0);
196108
}
197109

198110
public function latestTimestamp(): int
199111
{
200-
return $this->getMetaData('latest_timestamp')
112+
return $this->getMetadata('latest_timestamp')
201113
?? (is_file($this->path) ? filemtime($this->path) : 0);
202114
}
203115

0 commit comments

Comments
 (0)