PHP code example of aledefreitas / zlx_cache

1. Go to this page and download the library: Download aledefreitas/zlx_cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aledefreitas / zlx_cache example snippets


 	use ZLX\Cache\Cache;
	$config = [ 'prefix' => 'cache_prefix',
				'instances' => [
					// Utilizamos default, pois é a configuração padrão do cache
					'default' => [	'engine' => 'memcached',
									'duration' => '+10 minutes',
									'groups' => [ 'Posts', 'Comments', 'Session' ],
									'prevent_clear' => [ 'Session' ] ]
				]
			];
	// Iniciamos o cache
 	Cache::init($config);

 	use ZLX\Cache\Cache;
	$config = [	'engine' => 'memcached',
				'duration' => '+10 minutes',
				'groups' => [ 'Posts', 'Comments', 'Session' ],
				'prevent_clear' => [ 'Session' ]
			];

 	Cache::create('meu_cache', $config);

		Cache::set('chave', 'teste de valor', 'default');

		Cache::get('chave', 'default');

		Cache::delete('chave', 'default');

		Cache::remember('chave', function() {
 		// Inclua sua lógica aqui
 		return $retorno;
		}, 'default');

		Cache::clearGroup('Grupo', 'default');

		Cache::clear(false, 'default');

		Cache::clearNamespace('Namespace');

		Cache::set("Posts.data.".$id_post, [ "title" => "Meus dados do post", "body" => "Corpo do meu post" ]);

use ZLX\Cache\CacheEngine;

	class CustomCacheEngine extends CacheEngine {
	public $_defaultConfigs; // Configurações padrões

	public function __construct(array $config) {
		// Lógica do método construtor

		$this->_configs = array_merge($this->_defaultConfigs, $config); // Merge das configurações padrões. É necessário caso haja configurações padrões.
		parent::__construct($config);
	}

	public function set($key, $value, $custom_ttl = false) {
		// Lógica de salvamento de valores no cache
	}

	public function get($key) {
		// Lógica de busca de valores no cache
	}

	public function delete($key) {
		// Lógica de apagamento de valor no cache
	}

	public function clear($ignore_prevents) {
		// Lógica para reset do cache
	}

	public function add($key, $value, $ttl = 3) {
		// Lógica para add no cache
	}
}

	// Através da inicialização do Cache
 	use ZLX\Cache\Cache;
	$config = [ 'prefix' => 'cache_prefix',
				'instances' => [
					'meu_cache' => [	'engine' => 'CustomCacheEngine',
										'duration' => '+10 minutes',
										'groups' => [ 'Posts', 'Comments', 'Session' ],
										'prevent_clear' => [ 'Session' ] ]
				]
			];

 	Cache::init($config);

	// Através da inicialização do Cache
 	use ZLX\Cache\Cache;
	$config = [	'engine' => 'CustomCacheEngine',
				'duration' => '+10 minutes',
				'groups' => [ 'Posts', 'Comments', 'Session' ],
				'prevent_clear' => [ 'Session' ]
			];

 	Cache::create('meu_cache', $config);