Skip to content

Commit 3ee0760

Browse files
authored
Merge pull request #32 from iazaran/31-feature-request-add-store-method-support
31 feature request add store method support
2 parents 5e1790d + c14233f commit 3ee0760

File tree

10 files changed

+613
-143
lines changed

10 files changed

+613
-143
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ smart_cache(['products' => $products], 3600);
6161
$products = smart_cache('products');
6262
```
6363

64+
### Using Different Cache Drivers
65+
66+
Use different cache drivers while maintaining all SmartCache optimizations:
67+
68+
```php
69+
// Use Redis with all SmartCache optimizations (compression, chunking, etc.)
70+
SmartCache::store('redis')->put('key', $value, 3600);
71+
SmartCache::store('redis')->get('key');
72+
73+
// Use Memcached with optimizations
74+
SmartCache::store('memcached')->remember('users', 3600, fn() => User::all());
75+
76+
// Use file cache with optimizations
77+
SmartCache::store('file')->put('config', $config, 86400);
78+
79+
// For raw access to Laravel's cache (bypasses SmartCache optimizations)
80+
SmartCache::repository('redis')->put('key', $value, 3600);
81+
```
82+
83+
> **Full Laravel Compatibility:** SmartCache implements Laravel's `Repository` interface, so it works seamlessly with any code that type-hints `Illuminate\Contracts\Cache\Repository`. The `store()` method returns a SmartCache instance that is also a valid Repository.
84+
6485
## 💡 Core Features (Automatic Optimization)
6586

6687
### 1. Intelligent Compression

docs/index.html

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,35 @@ <h2>Familiar Laravel API</h2>
763763
// Clear all cache
764764
SmartCache::flush();</code></pre>
765765

766+
<h2>Using Different Cache Drivers</h2>
767+
<p>Use different cache drivers while maintaining all SmartCache optimizations:</p>
768+
769+
<pre><code class="language-php">// Use Redis with all SmartCache optimizations (compression, chunking, etc.)
770+
SmartCache::store('redis')->put('key', $value, 3600);
771+
SmartCache::store('redis')->get('key');
772+
773+
// Use Memcached with optimizations
774+
SmartCache::store('memcached')->remember('users', 3600, fn() => User::all());
775+
776+
// Use file cache with optimizations
777+
SmartCache::store('file')->put('config', $config, 86400);
778+
779+
// Chain multiple operations on a specific store
780+
$redisCache = SmartCache::store('redis');
781+
$redisCache->put('users', $users, 3600);
782+
$redisCache->put('products', $products, 3600);
783+
784+
// For raw access to Laravel's cache (bypasses SmartCache optimizations)
785+
SmartCache::repository('redis')->put('key', $value, 3600);</code></pre>
786+
787+
<div class="alert alert-info">
788+
<strong>Note:</strong> The <code>store()</code> method returns a SmartCache instance, so all optimization strategies (compression, chunking, encryption, etc.) continue to work. Use <code>repository()</code> if you need direct access to Laravel's cache without SmartCache optimizations.
789+
</div>
790+
791+
<div class="alert alert-success">
792+
<strong>Full Laravel Compatibility:</strong> SmartCache implements Laravel's <code>Illuminate\Contracts\Cache\Repository</code> interface, so it works seamlessly with any code that type-hints <code>Repository</code>. The <code>store()</code> method returns a SmartCache instance that is also a valid Repository, ensuring zero breaking changes when migrating from Laravel's Cache facade.
793+
</div>
794+
766795
<h2>Automatic Optimization</h2>
767796
<p>SmartCache automatically optimizes your data when beneficial:</p>
768797

@@ -1712,6 +1741,61 @@ <h2 id="basic-ops">🔧 Basic Cache Operations</h2>
17121741
</div>
17131742
</div>
17141743

1744+
<div class="method">
1745+
<div class="method-name">SmartCache::store() <span class="badge badge-success">Enhanced!</span></div>
1746+
<div class="method-signature">SmartCache::store(string|null $name = null): static</div>
1747+
<p>Get a SmartCache instance for a specific cache driver. All SmartCache optimizations (compression, chunking, encryption, etc.) are preserved. The returned instance also implements <code>Illuminate\Contracts\Cache\Repository</code> for full Laravel compatibility.</p>
1748+
<div class="parameter">
1749+
<span class="parameter-name">$name</span> <span class="parameter-type">(string|null)</span> - The cache store name (redis, file, memcached, etc.)
1750+
</div>
1751+
<div class="parameter">
1752+
<span class="return-type">Returns:</span> static - SmartCache instance configured for the specified store (also implements Repository)
1753+
</div>
1754+
<div class="example">
1755+
<strong>Examples:</strong>
1756+
<pre><code class="language-php">// Use Redis with all SmartCache optimizations
1757+
SmartCache::store('redis')->put('key', $value, 3600);
1758+
SmartCache::store('redis')->get('key');
1759+
1760+
// Use Memcached with optimizations
1761+
SmartCache::store('memcached')->remember('users', 3600, fn() => User::all());
1762+
1763+
// Chain operations on a specific store
1764+
$redisCache = SmartCache::store('redis');
1765+
$redisCache->put('users', $users, 3600);
1766+
$redisCache->put('products', $products, 3600);
1767+
1768+
// Works with Repository type hints
1769+
function cacheData(\Illuminate\Contracts\Cache\Repository $cache) {
1770+
$cache->put('key', 'value', 3600);
1771+
}
1772+
cacheData(SmartCache::store('redis')); // ✅ Works!</code></pre>
1773+
</div>
1774+
</div>
1775+
1776+
<div class="method">
1777+
<div class="method-name">SmartCache::repository() <span class="badge badge-success">New!</span></div>
1778+
<div class="method-signature">SmartCache::repository(string|null $name = null): \Illuminate\Contracts\Cache\Repository</div>
1779+
<p>Get direct access to Laravel's underlying cache repository. This bypasses all SmartCache optimizations.</p>
1780+
<div class="parameter">
1781+
<span class="parameter-name">$name</span> <span class="parameter-type">(string|null)</span> - The cache store name (redis, file, memcached, etc.)
1782+
</div>
1783+
<div class="parameter">
1784+
<span class="return-type">Returns:</span> Repository - Laravel's native cache repository
1785+
</div>
1786+
<div class="example">
1787+
<strong>Examples:</strong>
1788+
<pre><code class="language-php">// Direct access to Redis cache (no SmartCache optimizations)
1789+
SmartCache::repository('redis')->put('key', $value, 3600);
1790+
1791+
// Direct access to default cache store
1792+
SmartCache::repository()->get('key');</code></pre>
1793+
</div>
1794+
<div class="alert alert-warning" style="margin-top: 10px;">
1795+
<strong>Note:</strong> Use <code>repository()</code> only when you need to bypass SmartCache optimizations. For normal usage, prefer <code>store()</code> to maintain all optimization benefits.
1796+
</div>
1797+
</div>
1798+
17151799
<h2 id="swr-api">🌊 SWR Patterns (Laravel 12+)</h2>
17161800

17171801
<div class="method">

src/Console/Commands/ClearCommand.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ protected function clearAllKeys(SmartCache $cache): int
112112

113113
protected function clearOrphanedKeys(SmartCache $cache): int
114114
{
115-
$repository = $cache->store();
116-
$store = $repository->getStore();
115+
$store = $cache->getStore();
117116
$cleared = 0;
118117
$managedKeys = $cache->getManagedKeys();
119118

@@ -122,7 +121,7 @@ protected function clearOrphanedKeys(SmartCache $cache): int
122121

123122
foreach ($allKeys as $key) {
124123
if (!\in_array($key, $managedKeys, true) && !$this->isSmartCacheInternalKey($key)) {
125-
if ($repository->forget($key)) {
124+
if ($cache->store()->forget($key)) {
126125
$cleared++;
127126
$this->line("Cleared key: {$key}");
128127
}

src/Console/Commands/StatusCommand.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ protected function displayConfiguration(ConfigRepository $config): void
123123

124124
protected function findAllNonManagedKeys(SmartCache $cache): array
125125
{
126-
$repository = $cache->store();
127-
$store = $repository->getStore();
126+
$store = $cache->getStore();
128127
$managedKeys = $cache->getManagedKeys();
129128
$nonManagedKeys = [];
130129

src/Contracts/SmartCache.php

Lines changed: 22 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,84 +2,38 @@
22

33
namespace SmartCache\Contracts;
44

5-
interface SmartCache
5+
use Illuminate\Contracts\Cache\Repository;
6+
7+
/**
8+
* SmartCache Contract
9+
*
10+
* This interface extends Laravel's Repository interface to ensure full compatibility
11+
* with Laravel's cache system while adding SmartCache-specific optimization features.
12+
*/
13+
interface SmartCache extends Repository
614
{
715
/**
8-
* Get an item from the cache.
16+
* Get a SmartCache instance using a specific cache store.
917
*
10-
* @param string $key
11-
* @param mixed $default
12-
* @return mixed
13-
*/
14-
public function get(string $key, mixed $default = null): mixed;
15-
16-
/**
17-
* Store an item in the cache.
18-
*
19-
* @param string $key
20-
* @param mixed $value
21-
* @param \DateTimeInterface|\DateInterval|int|null $ttl
22-
* @return bool
23-
*/
24-
public function put(string $key, mixed $value, $ttl = null): bool;
25-
26-
/**
27-
* Determine if an item exists in the cache.
28-
*
29-
* @param string $key
30-
* @return bool
31-
*/
32-
public function has(string $key): bool;
33-
34-
/**
35-
* Remove an item from the cache.
36-
*
37-
* @param string $key
38-
* @return bool
39-
*/
40-
public function forget(string $key): bool;
41-
42-
/**
43-
* Store an item in the cache indefinitely.
44-
*
45-
* @param string $key
46-
* @param mixed $value
47-
* @return bool
48-
*/
49-
public function forever(string $key, mixed $value): bool;
50-
51-
/**
52-
* Get an item from the cache, or execute the given Closure and store the result.
53-
*
54-
* @param string $key
55-
* @param \DateTimeInterface|\DateInterval|int|null $ttl
56-
* @param \Closure $callback
57-
* @return mixed
58-
*/
59-
public function remember(string $key, $ttl, \Closure $callback): mixed;
60-
61-
/**
62-
* Get an item from the cache, or execute the given Closure and store the result forever.
18+
* When called without arguments, returns the current instance.
19+
* When called with a store name, returns a new SmartCache instance
20+
* configured to use that store while maintaining all optimization strategies.
6321
*
64-
* @param string $key
65-
* @param \Closure $callback
66-
* @return mixed
22+
* @param string|null $name The cache store name (e.g., 'redis', 'file', 'memcached')
23+
* @return static
6724
*/
68-
public function rememberForever(string $key, \Closure $callback): mixed;
25+
public function store(string|null $name = null): static;
6926

7027
/**
71-
* Get the underlying cache store.
28+
* Get the underlying cache repository directly.
7229
*
73-
* @return \Illuminate\Contracts\Cache\Repository
74-
*/
75-
public function store(string|null $name = null): \Illuminate\Contracts\Cache\Repository;
76-
77-
/**
78-
* Clear all cache keys managed by SmartCache.
30+
* This provides raw access to Laravel's cache repository without SmartCache optimizations.
31+
* Use this when you need direct access to the cache driver.
7932
*
80-
* @return bool
33+
* @param string|null $name The store name (null for current store)
34+
* @return Repository
8135
*/
82-
public function clear(): bool;
36+
public function repository(string|null $name = null): Repository;
8337

8438
/**
8539
* Get all keys managed by SmartCache.
@@ -291,14 +245,6 @@ public function many(array $keys): array;
291245
*/
292246
public function putMany(array $values, $ttl = null): bool;
293247

294-
/**
295-
* Remove multiple items from the cache.
296-
*
297-
* @param array $keys
298-
* @return bool
299-
*/
300-
public function deleteMultiple(array $keys): bool;
301-
302248
/**
303249
* Get a memoized cache instance.
304250
*

src/Facades/SmartCache.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
* @method static mixed swr(string $key, \Closure $callback, int $ttl = 3600, int $staleTtl = 7200)
1717
* @method static mixed stale(string $key, \Closure $callback, int $ttl = 1800, int $staleTtl = 86400)
1818
* @method static mixed refreshAhead(string $key, \Closure $callback, int $ttl = 3600, int $refreshWindow = 600)
19-
* @method static \Illuminate\Contracts\Cache\Repository store(string|null $name = null)
19+
* @method static \SmartCache\SmartCache store(string|null $name = null)
20+
* @method static \Illuminate\Contracts\Cache\Repository repository(string|null $name = null)
2021
* @method static bool clear()
2122
* @method static array getManagedKeys()
2223
* @method static static tags(string|array $tags)
@@ -35,7 +36,7 @@
3536
* @method static array analyzePerformance()
3637
* @method static int cleanupExpiredManagedKeys()
3738
* @method static bool hasFeature(string $feature)
38-
*
39+
*
3940
* @see \SmartCache\SmartCache
4041
*/
4142
class SmartCache extends Facade

0 commit comments

Comments
 (0)