nodes-php / cache by nodes

Makes it easier to control and manage Laravel caching
13,617
7
2
Package Data
Maintainer Username: nodes
Maintainer Contact: cr@nodes.dk (Casper Rasmussen)
Package Create Date: 2015-11-08
Package Last Update: 2020-04-24
Language: PHP
License: Unknown
Last Refreshed: 2024-03-24 03:17:52
Package Statistics
Total Downloads: 13,617
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 7
Total Watchers: 2
Total Forks: 4
Total Open Issues: 0

Cache

An easy integration for handling caching - in a structured way - for Laravel.

Cache keys often build up and ends up being 3+ params attenpended in a string. Or caching on urls will make different order of query params have different version of same cache. Query params are often handled as hashmaps or dictionaries, which does not have a locked order

The worst part is, you end up with having cache settings shattered all over your project.

This package will make your life much easier

Total downloads Monthly downloads Latest release Open issues License Star repository on GitHub Watch repository on GitHub Fork repository on GitHub StyleCI

📝 Introduction

One thing we at Nodes have been missing a lot in Laravel is more structure of managing caches.

We've come up with a more flexible and structured way of managing caches and their lifetime. Also we've created a few helper methods to make it all a bit easier and awesome.

📦 Installation

To install this package you will need:

  • Laravel 5.1+
  • PHP 5.5.9+

You must then modify your composer.json file and run composer update to include the latest version of the package in your project.

"require": {
    "nodes/cache": "^1.0"
}

Or you can run the composer require command from your terminal.

composer require nodes/cache:^1.0

🔧 Setup

Setup service provider in config/app.php

Nodes\Cache\ServiceProvider::class

Setup alias in config/app.php

'NodesCache' => Nodes\Cache\Support\Facades\Cache::class

Publish config file

php artisan vendor:publish --provider="Nodes\Cache\ServiceProvider"

If you want to overwrite any existing config files use the --force parameter

php artisan vendor:publish --provider="Nodes\Cache\ServiceProvider" --force

⚙ Usage

Global methods

function cache_remember($cacheGroupKey, array $params = [], $data, $tags = null, \Closure $closure = null)
function cache_put($cacheGroupKey, array $params = [], $data, $tags = null)
function cache_get($cacheGroupKey, array $params = [], $tags = null)
function cache_forget($cacheGroupKey, array $params = [], $tags = null)
function cache_flush($tags)
function cache_wipe()

Facade methods

\NodesCache::remember($cacheGroupKey, array $params = [], $data, $tags = null, \Closure $closure = null)
\NodesCache::put($cacheGroupKey, array $params = [], $data, $tags = null)
\NodesCache::get($cacheGroupKey, array $params = [], $tags = null)
\NodesCache::forget($cacheGroupKey, array $params = [], $tags = null)
\NodesCache::flush($tags)
\NodesCache::wipe()

Examples

First important thing is to create the config for you new cache group, config should be in /config/nodes/cache.php (else you forgot to vendor:publish)

'groups'   => [
/*
|--------------------------------------------------------------------------
| Project
|--------------------------------------------------------------------------
|
| Cache settings used by your project.
|
*/
'geographic' => [
    'continent.bySlug' => [
	'active'   => true,
	'key'      => 'geographic-continent-by-slug',
	'lifetime' => 3600,
    ],
...

Will give you a $cacheGroup geographic.continent.bySlug Remember to make the key unique to avoid conflicts

Remember

Remember is a way to both get and put to cache, 95% of cases this will be the right choice

return cache_remember('geographic.continent.bySlug', ['slug' => $slug], $tags, function () use ($slug) {

	// Look up in db
	$continent = $this->where('slug', $slug)->first();

	if (!$continent) {
		throw new EntityNotFoundException(sprintf('Could not find continent with slug [%s]', $slug));
	}

	return $continent;
});

This way we start by looking in cache, if data if found it will be returned. Else the closure will run where we look up the data in db and return. Returning it in closure will then cache it and return it.

Put

Just put data in cache

// Look up in db
$continent = $this->where('slug', $slug)->first();
if (!$continent) {
	throw new EntityNotFoundException(sprintf('Could not find continent with slug [%s]', $slug));
}

// Put in cache
$success = cache_put('geographic.continent.bySlug', ['slug' => $slug], $continent)

$success is a bool

Get

Just get data from cache

// Get from cache
$continent cache_get('geographic.continent.bySlug', ['slug' => $slug])

$continent is the continent or null

🏆 Credits

This package is developed and maintained by the PHP team at Nodes

Follow Nodes PHP on Twitter Tweet Nodes PHP

📄 License

This package is open-sourced software licensed under the MIT license