| Package Data | |
|---|---|
| Maintainer Username: | nnjeim | 
| Maintainer Contact: | nnjeim@nnjeim.com (Najm Njeim) | 
| Package Create Date: | 2021-09-13 | 
| Package Last Update: | 2021-09-18 | 
| Home Page: | |
| Language: | PHP | 
| License: | MIT | 
| Last Refreshed: | 2025-10-30 15:01:33 | 
| Package Statistics | |
|---|---|
| Total Downloads: | 50 | 
| Monthly Downloads: | 0 | 
| Daily Downloads: | 0 | 
| Total Stars: | 1 | 
| Total Watchers: | 1 | 
| Total Forks: | 0 | 
| Total Open Issues: | 0 | 
A Laravel cache helper methods.
You can install the package via composer:
composer require nnjeim/persist
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();
	}
}
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();
	}
}
Sets the cache tag string | array
@return $this       setCacheTag(string | array $cacheTag, $suffix = null)
Sets the cache key string 
@return $this       setCacheKey(string $cacheKey)
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, ...)
Asserts if a cache key exists
@return bool       hasCacheKey(string $cacheKey)
Returns the peristed cache key.
@return mixed       getCacheKey(string $cacheKey)
returns the cached value for a given number of seconds
@return mixed       rememberCache($value, $ttl)
returns the cached value
@return mixed       rememberCacheForever($value)
Clears the cache of the given cache key.   
@return void       forgetCacheKey(string $cacheKey)
Clears all the cache keys related to a given cache tag
@return void       flushCacheTag()
Increments a cache key by a given value. default = 1.
@return void       incrementCacheKey($amount = 1)
Decrements a cache key by a given value. default = 1.
@return void       decrementCacheKey($amount = 1)
Please see CHANGELOG for more information what has changed recently.