PHPackages                             uxmansarwar/cache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. uxmansarwar/cache

ActiveLibrary[Caching](/categories/caching)

uxmansarwar/cache
=================

Laravel-like Cache Wrapper for Core PHP using Symfony Cache

v1.0.1(1y ago)16MITPHPPHP &gt;=8.0

Since Apr 18Pushed 1y ago1 watchersCompare

[ Source](https://github.com/uxmansarwar/cache)[ Packagist](https://packagist.org/packages/uxmansarwar/cache)[ RSS](/packages/uxmansarwar-cache/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (3)Versions (5)Used By (0)

🧰 Laravel-Like Cache Wrapper for Core PHP
=========================================

[](#-laravel-like-cache-wrapper-for-core-php)

A **fully-featured Laravel-style Cache Wrapper** built in **Core PHP** using the power of Symfony's Cache component. This package provides an elegant and fluent caching API that mirrors Laravel's `Cache` facade, making it easy for developers to manage caching in procedural or object-oriented PHP applications outside of Laravel.

---

📌 Why This Package?
-------------------

[](#-why-this-package)

Laravel provides a very expressive and flexible API for caching, but it's deeply coupled with the Laravel framework. This package allows you to bring that **elegant Laravel cache experience** into any Core PHP project without requiring Laravel at all.

**Built for:**

- Developers transitioning from Laravel to raw PHP
- Lightweight or micro PHP apps that need efficient caching
- Custom frameworks and standalone apps

---

🧠 Why a Wrapper?
----------------

[](#-why-a-wrapper)

Wrapping an existing library like Symfony's Cache component helps abstract complexity and create a **clean, readable, Laravel-style API** without reinventing the wheel. Symfony's cache is PSR-6/16 compliant and battle-tested in production, making it an excellent backend for this wrapper.

---

⚙️ Requirements
---------------

[](#️-requirements)

- PHP 8.0 or later
- Composer
- Symfony Cache Component

Install Symfony Cache:

```
composer require symfony/cache
```

---

📦 Installation
--------------

[](#-installation)

### 1. Clone or Require

[](#1-clone-or-require)

If you're using Composer for your project:

```
composer require uxmansarwar/cache
```

If you're manually integrating:

```
git clone https://github.com/uxmansarwar/cache.git
```

Place `src/Cache.php` in your preferred directory and include it with Composer autoload.

---

🧪 Initialization
----------------

[](#-initialization)

Before using the cache, initialize it:

```
use UxmanSarwar\Cache;

Cache::init(
    namespace: 'my_app',
    defaultLifetime: 3600,
    directory: __DIR__ . '/storage/cache'
);
```

---

🛠 Full API Usage Examples
-------------------------

[](#-full-api-usage-examples)

### 🔍 Get Value from Cache

[](#-get-value-from-cache)

```
$value = Cache::get('user_1');
```

### 📝 Put Value into Cache

[](#-put-value-into-cache)

```
Cache::put('user_1', ['name' => 'John'], 600); // 10 mins
```

### ♾️ Store Permanently

[](#️-store-permanently)

```
Cache::forever('site_name', 'My Awesome App');
```

### ❓ Check if Key Exists

[](#-check-if-key-exists)

```
if (Cache::has('user_1')) {
    echo "User found!";
}
```

### ❌ Forget a Cache Key

[](#-forget-a-cache-key)

```
Cache::forget('user_1');
```

### 🧹 Flush Entire Cache

[](#-flush-entire-cache)

```
Cache::flush();
```

### 🦁 Remember (Lazy Load + Cache)

[](#-remember-lazy-load--cache)

```
$data = Cache::remember('config_data', 3600, function () {
    return expensiveFetch();
});
```

### 🦁 Remember Forever

[](#-remember-forever)

```
$data = Cache::rememberForever('constants', function () {
    return loadConstants();
});
```

### 🧪 Pull and Delete

[](#-pull-and-delete)

```
$data = Cache::pull('temp_code'); // returns and deletes it
```

### 🪮 Add if Not Exists

[](#-add-if-not-exists)

```
$wasAdded = Cache::add('otp_123', 456789, 120);
```

### 🕾️ Increment a Value

[](#️-increment-a-value)

```
$newValue = Cache::increment('views', 1);
```

### 🔾 Decrement a Value

[](#-decrement-a-value)

```
$newValue = Cache::decrement('downloads', 2);
```

---

🧱 Tests &amp; Development Setup
-------------------------------

[](#-tests--development-setup)

### Install Dev Dependencies

[](#install-dev-dependencies)

```
composer require --dev pestphp/pest phpstan/phpstan
```

### PestPHP Setup

[](#pestphp-setup)

```
./vendor/bin/pest --init
```

This will create the `tests/` directory and `Pest.php` bootstrap file.

### PHPStan Setup

[](#phpstan-setup)

Create `phpstan.neon`:

```
includes:
    - vendor/phpstan/phpstan/conf/bleedingEdge.neon

parameters:
    level: 8
    paths:
        - src
        - tests
```

### Add Test Scripts to `composer.json`

[](#add-test-scripts-to-composerjson)

```
"scripts": {
    "test": "pest",
    "stan": "phpstan analyse"
}
```

---

📚 Example Pest Tests
--------------------

[](#-example-pest-tests)

File: `tests/CacheTest.php`

```
