nnjeim/persist

Laravel cache helper methods

Installs: 14

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

Type:laravel-package

1.0.1 2021-09-18 10:19 UTC

This package is auto-updated.

Last update: 2021-10-11 05:08:18 UTC


README

68747470733a2f2f65752e75692d617661746172732e636f6d2f6170692f3f6e616d653d4e616a6d2b4e6a65696d3f73697a653d313030

nnjeim Persist Helper

A Laravel cache helper methods.

Installation

You can install the package via composer:

composer require nnjeim/persist

Usage

Persist Facade
use Nnjeim\Fetch\Fetch;
use Nnjeim\Respond\Respond;
use Nnjeim\Persist\Persist;

class Country { 

	public function index() {

		if (Persist::setCacheTag('countries')->setCacheKey('index')->hasCacheKey()) {
		
			return Respond::toJson()
				->setMessage('countries')
				->setData(
					Persist::setCacheTag('countries')
						->setCacheKey('index')
						->getCacheKey()
				)
				->withSuccess();
		}
		
		['response' => $response, 'status' => $status] = Fetch::setBaseUri('https://someapi.com')->get('countries');
		
			if ($status === 200 && $response->success) {
				
				$data = Persist::setCacheTag('countries')
					->setCacheKey('index')
					->rememberCacheForever($response->data);H
		
				return Respond::toJson()
					->setMessage('countries')
					->setData($data)
					->withSuccess();
			}
		
			return Respond::withErrors();
	}
}
PersistHelper Instantiation
use Nnjeim\Persist\PersistHelper;
use Nnjeim\Fetch\FetchHelper;
use Nnjeim\Respond\RespondHelper;

class Country { 

	private PersistHelper $persist;
	private FetchHelper $fetch;
	private RespondHelper $respond;

	public function __construct(
		PersistHelper $persist, 
		FetchHelper $fetch, 
		RespondHelper $respond
		) {
	
		$this->persist = $persist;
		$this->fetch = $fetch;
		$this->respond = $respond;
	
		$this->persist->setCacheTag = 'countries';
		$this->fetch->setBaseUri = 'https://someapi.com';
	}
	
	public function index() {
	
		$this->persist->setCacheKey = 'index';
	
		if ($this->persist->hasCacheKey()) {
			
			return $this->respond
				->toJson()
				->setData($this->persist->getCacheKey())
				->withSuccess();    
		}
	
		['response' => $response, 'status' => $status] = $this->fetch->get('countries');
	
		if ($status === 200 && $response->success) {
			
			$data = $this->persist->rememberCacheForever($response->data);
	
			return $this->respond
                ->toJson()
                ->setData($data)
                ->withSuccess();    
		}
	
		return $this->respond->withErrors();
	}
}

Methods

Set the cache tag
Sets the cache tag string | array

@return $this       setCacheTag(string | array $cacheTag, $suffix = null)
Set the cache key
Sets the cache key string 

@return $this       setCacheKey(string $cacheKey)
Form a composite cache key
sets the cache key with the result of the concatenatenation of multiple strings with an '_' seprator.

@return $this       formCacheKey(array $strings) or formCacheKey($string1, $string2, ...)
Cache Key exist
Asserts if a cache key exists

@return bool       hasCacheKey(string $cacheKey)
Get a cache key
Returns the peristed cache key.

@return mixed       getCacheKey(string $cacheKey)
Remember cache
returns the cached value for a given number of seconds

@return mixed       rememberCache($value, $ttl)
Remember cache forever
returns the cached value

@return mixed       rememberCacheForever($value)
Forget a cache key
Clears the cache of the given cache key.   

@return void       forgetCacheKey(string $cacheKey)
Flush the cache by Tag
Clears all the cache keys related to a given cache tag

@return void       flushCacheTag()
Increment a cache key
Increments a cache key by a given value. default = 1.

@return void       incrementCacheKey($amount = 1)
Decrement a cache key
Decrements a cache key by a given value. default = 1.

@return void       decrementCacheKey($amount = 1)

Changelog

Please see CHANGELOG for more information what has changed recently.