1 2/** 3 * This file is part of the Phalcon Framework. 4 * 5 * (c) Phalcon Team <team@phalcon.io> 6 * 7 * For the full copyright and license information, please view the LICENSE.txt 8 * file that was distributed with this source code. 9 */ 10 11namespace Phalcon\Cache; 12 13use Phalcon\Cache\Adapter\AdapterInterface; 14use Phalcon\Cache; 15use Psr\SimpleCache\CacheInterface; 16use Phalcon\Cache\Exception\Exception; 17use Phalcon\Config; 18use Phalcon\Config\ConfigInterface; 19use Phalcon\Helper\Arr; 20 21/** 22 * Creates a new Cache class 23 */ 24class CacheFactory 25{ 26 /** 27 * @var AdapterFactory 28 */ 29 protected adapterFactory; 30 31 /** 32 * Constructor 33 */ 34 public function __construct(<AdapterFactory> factory) 35 { 36 let this->adapterFactory = factory; 37 } 38 39 /** 40 * Factory to create an instance from a Config object 41 * 42 * @param array|\Phalcon\Config config = [ 43 * 'adapter' => 'apcu', 44 * 'options' => [ 45 * 'servers' => [ 46 * [ 47 * 'host' => 'localhost', 48 * 'port' => 11211, 49 * 'weight' => 1, 50 * 51 * ] 52 * ], 53 * 'host' => '127.0.0.1', 54 * 'port' => 6379, 55 * 'index' => 0, 56 * 'persistent' => false, 57 * 'auth' => '', 58 * 'socket' => '', 59 * 'defaultSerializer' => 'Php', 60 * 'lifetime' => 3600, 61 * 'serializer' => null, 62 * 'prefix' => 'phalcon', 63 * 'storageDir' => '' 64 * ] 65 * ] 66 */ 67 public function load(var config) -> var 68 { 69 var name, options; 70 71 if typeof config == "object" && config instanceof ConfigInterface { 72 let config = config->toArray(); 73 } 74 75 if unlikely typeof config !== "array" { 76 throw new Exception( 77 "Config must be array or Phalcon\\Config object" 78 ); 79 } 80 81 if unlikely !isset config["adapter"] { 82 throw new Exception( 83 "You must provide 'adapter' option in factory config parameter." 84 ); 85 } 86 87 let name = config["adapter"], 88 options = Arr::get(config, "options", []); 89 90 return this->newInstance(name, options); 91 } 92 93 /** 94 * Constructs a new Cache instance. 95 * 96 * @param array options = [ 97 * 'servers' => [ 98 * [ 99 * 'host' => 'localhost', 100 * 'port' => 11211, 101 * 'weight' => 1, 102 103 * ] 104 * ], 105 * 'host' => '127.0.0.1', 106 * 'port' => 6379, 107 * 'index' => 0, 108 * 'persistent' => false, 109 * 'auth' => '', 110 * 'socket' => '', 111 * 'defaultSerializer' => 'Php', 112 * 'lifetime' => 3600, 113 * 'serializer' => null, 114 * 'prefix' => 'phalcon', 115 * 'storageDir' => '' 116 * ] 117 */ 118 public function newInstance(string! name, array! options = []) -> <CacheInterface> 119 { 120 var adapter; 121 122 let adapter = this->adapterFactory->newInstance(name, options); 123 124 return new Cache(adapter); 125 } 126} 127