PHP code example of jlzan1314 / swoft-cache

1. Go to this page and download the library: Download jlzan1314/swoft-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/ */

    

jlzan1314 / swoft-cache example snippets


//bean.php

'cache.group2'=>[
    'class'=>\Jlzan1314\Cache\Group::class,
    'driver'=>bean("cache.driver"),
    'ttl'=>500,//默认的ttl
],


$cache=bean('cache');

 declare(strict_types=1);


namespace App\Http\Controller;

use Jlzan1314\Cache\Annotation\Mapping\Cacheable;
use Jlzan1314\Cache\Annotation\Mapping\CacheEvict;
use Jlzan1314\Cache\Annotation\Mapping\CachePut;
use Jlzan1314\Cache\Listener\DeleteListenerEvent;
use ReflectionException;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;

/**
 * Class LogController
 *
 * @since 2.0
 *
 * @Controller("test")
 */
class TestController
{

	/**
	 * @RequestMapping("cache")
	 * 缓存使用
	 * @throws ReflectionException
	 * @throws ContainerException
	 */
	public function cache()
	{
		$cache=$this->cacheable(1);
		$cache2=$this->cachePut(1,2);
		$cacheGroup=$this->cacheGroup();
		return compact('cache','cache2','cacheGroup');
	}


	/**
     * 清除缓存
	 * @RequestMapping("delCache")
	 * 
	 * @throws ReflectionException
	 * @throws ContainerException
	 */
	public function delCache(){
		\Swoft::trigger(new DeleteListenerEvent('deleteDdd',[1]));
		\Swoft::trigger(new DeleteListenerEvent('deleteGroup'));
		$this->cacheEvict(1,2);
		return "delete success";
	}

	/**
	 * @Cacheable(prefix="ddd",ttl=3600,listener="deleteDdd")
	 */
	public function cacheable($id){
		return date("Y-m-d H:i:s");
	}

	/**
	 * @CachePut(prefix="put",ttl=300,value="#{id}:#{arg2}")
	 * @param $id
	 */
	public function cachePut($id,$arg2){
		return date("Y-m-d H:i:s");
	}

	/**
	 * 清除缓存信息
	 * @CacheEvict(prefix="put", value="#{id}:#{arg2}")
	 */
	public function cacheEvict($id,$arg2){
		return true;
	}

	/**
	 * 清除缓存信息
	 * @Cacheable(prefix="group",group="cache.group2",listener="deleteGroup")
	 */
	public function cacheGroup(){
		return date("Y-m-d H:i:s");
	}
}