Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ smart_cache(['products' => $products], 3600);
$products = smart_cache('products');
```

### Using Different Cache Drivers

Use different cache drivers while maintaining all SmartCache optimizations:

```php
// Use Redis with all SmartCache optimizations (compression, chunking, etc.)
SmartCache::store('redis')->put('key', $value, 3600);
SmartCache::store('redis')->get('key');

// Use Memcached with optimizations
SmartCache::store('memcached')->remember('users', 3600, fn() => User::all());

// Use file cache with optimizations
SmartCache::store('file')->put('config', $config, 86400);

// For raw access to Laravel's cache (bypasses SmartCache optimizations)
SmartCache::repository('redis')->put('key', $value, 3600);
```

> **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.

## 💡 Core Features (Automatic Optimization)

### 1. Intelligent Compression
Expand Down
84 changes: 84 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,35 @@ <h2>Familiar Laravel API</h2>
// Clear all cache
SmartCache::flush();</code></pre>

<h2>Using Different Cache Drivers</h2>
<p>Use different cache drivers while maintaining all SmartCache optimizations:</p>

<pre><code class="language-php">// Use Redis with all SmartCache optimizations (compression, chunking, etc.)
SmartCache::store('redis')->put('key', $value, 3600);
SmartCache::store('redis')->get('key');

// Use Memcached with optimizations
SmartCache::store('memcached')->remember('users', 3600, fn() => User::all());

// Use file cache with optimizations
SmartCache::store('file')->put('config', $config, 86400);

// Chain multiple operations on a specific store
$redisCache = SmartCache::store('redis');
$redisCache->put('users', $users, 3600);
$redisCache->put('products', $products, 3600);

// For raw access to Laravel's cache (bypasses SmartCache optimizations)
SmartCache::repository('redis')->put('key', $value, 3600);</code></pre>

<div class="alert alert-info">
<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.
</div>

<div class="alert alert-success">
<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.
</div>

<h2>Automatic Optimization</h2>
<p>SmartCache automatically optimizes your data when beneficial:</p>

Expand Down Expand Up @@ -1712,6 +1741,61 @@ <h2 id="basic-ops">🔧 Basic Cache Operations</h2>
</div>
</div>

<div class="method">
<div class="method-name">SmartCache::store() <span class="badge badge-success">Enhanced!</span></div>
<div class="method-signature">SmartCache::store(string|null $name = null): static</div>
<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>
<div class="parameter">
<span class="parameter-name">$name</span> <span class="parameter-type">(string|null)</span> - The cache store name (redis, file, memcached, etc.)
</div>
<div class="parameter">
<span class="return-type">Returns:</span> static - SmartCache instance configured for the specified store (also implements Repository)
</div>
<div class="example">
<strong>Examples:</strong>
<pre><code class="language-php">// Use Redis with all SmartCache optimizations
SmartCache::store('redis')->put('key', $value, 3600);
SmartCache::store('redis')->get('key');

// Use Memcached with optimizations
SmartCache::store('memcached')->remember('users', 3600, fn() => User::all());

// Chain operations on a specific store
$redisCache = SmartCache::store('redis');
$redisCache->put('users', $users, 3600);
$redisCache->put('products', $products, 3600);

// Works with Repository type hints
function cacheData(\Illuminate\Contracts\Cache\Repository $cache) {
$cache->put('key', 'value', 3600);
}
cacheData(SmartCache::store('redis')); // ✅ Works!</code></pre>
</div>
</div>

<div class="method">
<div class="method-name">SmartCache::repository() <span class="badge badge-success">New!</span></div>
<div class="method-signature">SmartCache::repository(string|null $name = null): \Illuminate\Contracts\Cache\Repository</div>
<p>Get direct access to Laravel's underlying cache repository. This bypasses all SmartCache optimizations.</p>
<div class="parameter">
<span class="parameter-name">$name</span> <span class="parameter-type">(string|null)</span> - The cache store name (redis, file, memcached, etc.)
</div>
<div class="parameter">
<span class="return-type">Returns:</span> Repository - Laravel's native cache repository
</div>
<div class="example">
<strong>Examples:</strong>
<pre><code class="language-php">// Direct access to Redis cache (no SmartCache optimizations)
SmartCache::repository('redis')->put('key', $value, 3600);

// Direct access to default cache store
SmartCache::repository()->get('key');</code></pre>
</div>
<div class="alert alert-warning" style="margin-top: 10px;">
<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.
</div>
</div>

<h2 id="swr-api">🌊 SWR Patterns (Laravel 12+)</h2>

<div class="method">
Expand Down
5 changes: 2 additions & 3 deletions src/Console/Commands/ClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ protected function clearAllKeys(SmartCache $cache): int

protected function clearOrphanedKeys(SmartCache $cache): int
{
$repository = $cache->store();
$store = $repository->getStore();
$store = $cache->getStore();
$cleared = 0;
$managedKeys = $cache->getManagedKeys();

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

foreach ($allKeys as $key) {
if (!\in_array($key, $managedKeys, true) && !$this->isSmartCacheInternalKey($key)) {
if ($repository->forget($key)) {
if ($cache->store()->forget($key)) {
$cleared++;
$this->line("Cleared key: {$key}");
}
Expand Down
3 changes: 1 addition & 2 deletions src/Console/Commands/StatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ protected function displayConfiguration(ConfigRepository $config): void

protected function findAllNonManagedKeys(SmartCache $cache): array
{
$repository = $cache->store();
$store = $repository->getStore();
$store = $cache->getStore();
$managedKeys = $cache->getManagedKeys();
$nonManagedKeys = [];

Expand Down
98 changes: 22 additions & 76 deletions src/Contracts/SmartCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,38 @@

namespace SmartCache\Contracts;

interface SmartCache
use Illuminate\Contracts\Cache\Repository;

/**
* SmartCache Contract
*
* This interface extends Laravel's Repository interface to ensure full compatibility
* with Laravel's cache system while adding SmartCache-specific optimization features.
*/
interface SmartCache extends Repository
{
/**
* Get an item from the cache.
* Get a SmartCache instance using a specific cache store.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get(string $key, mixed $default = null): mixed;

/**
* Store an item in the cache.
*
* @param string $key
* @param mixed $value
* @param \DateTimeInterface|\DateInterval|int|null $ttl
* @return bool
*/
public function put(string $key, mixed $value, $ttl = null): bool;

/**
* Determine if an item exists in the cache.
*
* @param string $key
* @return bool
*/
public function has(string $key): bool;

/**
* Remove an item from the cache.
*
* @param string $key
* @return bool
*/
public function forget(string $key): bool;

/**
* Store an item in the cache indefinitely.
*
* @param string $key
* @param mixed $value
* @return bool
*/
public function forever(string $key, mixed $value): bool;

/**
* Get an item from the cache, or execute the given Closure and store the result.
*
* @param string $key
* @param \DateTimeInterface|\DateInterval|int|null $ttl
* @param \Closure $callback
* @return mixed
*/
public function remember(string $key, $ttl, \Closure $callback): mixed;

/**
* Get an item from the cache, or execute the given Closure and store the result forever.
* When called without arguments, returns the current instance.
* When called with a store name, returns a new SmartCache instance
* configured to use that store while maintaining all optimization strategies.
*
* @param string $key
* @param \Closure $callback
* @return mixed
* @param string|null $name The cache store name (e.g., 'redis', 'file', 'memcached')
* @return static
*/
public function rememberForever(string $key, \Closure $callback): mixed;
public function store(string|null $name = null): static;

/**
* Get the underlying cache store.
* Get the underlying cache repository directly.
*
* @return \Illuminate\Contracts\Cache\Repository
*/
public function store(string|null $name = null): \Illuminate\Contracts\Cache\Repository;

/**
* Clear all cache keys managed by SmartCache.
* This provides raw access to Laravel's cache repository without SmartCache optimizations.
* Use this when you need direct access to the cache driver.
*
* @return bool
* @param string|null $name The store name (null for current store)
* @return Repository
*/
public function clear(): bool;
public function repository(string|null $name = null): Repository;

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

/**
* Remove multiple items from the cache.
*
* @param array $keys
* @return bool
*/
public function deleteMultiple(array $keys): bool;

/**
* Get a memoized cache instance.
*
Expand Down
5 changes: 3 additions & 2 deletions src/Facades/SmartCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
* @method static mixed swr(string $key, \Closure $callback, int $ttl = 3600, int $staleTtl = 7200)
* @method static mixed stale(string $key, \Closure $callback, int $ttl = 1800, int $staleTtl = 86400)
* @method static mixed refreshAhead(string $key, \Closure $callback, int $ttl = 3600, int $refreshWindow = 600)
* @method static \Illuminate\Contracts\Cache\Repository store(string|null $name = null)
* @method static \SmartCache\SmartCache store(string|null $name = null)
* @method static \Illuminate\Contracts\Cache\Repository repository(string|null $name = null)
* @method static bool clear()
* @method static array getManagedKeys()
* @method static static tags(string|array $tags)
Expand All @@ -35,7 +36,7 @@
* @method static array analyzePerformance()
* @method static int cleanupExpiredManagedKeys()
* @method static bool hasFeature(string $feature)
*
*
* @see \SmartCache\SmartCache
*/
class SmartCache extends Facade
Expand Down
Loading