| 101 | | |
|---|
| | 101 | |
|---|
| | 102 | |
|---|
| | 103 | /** |
|---|
| | 104 | * Instantiates and configures the AkCache store. |
|---|
| | 105 | * |
|---|
| | 106 | * If $options == NULL the configuration will be taken from the constants: |
|---|
| | 107 | * |
|---|
| | 108 | * AK_CACHE_HANDLER and AK_CACHE_OPTIONS |
|---|
| | 109 | * |
|---|
| | 110 | * if $options is of type string/int the $options parameter will be considered |
|---|
| | 111 | * as the AK_CACHE_HANDLER_* Type (AK_CACHE_HANDLER_PEAR,AK_CACHE_HANDLER_ADODB,AK_CACHE_HANDLER_MEMCACHE) |
|---|
| | 112 | * |
|---|
| | 113 | * if $options is an array of format: |
|---|
| | 114 | * |
|---|
| | 115 | * array('file'=>array('cacheDir'=>'/tmp')) |
|---|
| | 116 | * |
|---|
| | 117 | * or |
|---|
| | 118 | * |
|---|
| | 119 | * array(AK_CACHE_HANDLER_PEAR=>array('cacheDir'=>'/tmp')) |
|---|
| | 120 | * |
|---|
| | 121 | * the first key will be used as the AK_CACHE_HANDLER_* Type |
|---|
| | 122 | * and the array as the config options |
|---|
| | 123 | * |
|---|
| | 124 | * Default behaviour is calling the method with the $options == null parameter: |
|---|
| | 125 | * |
|---|
| | 126 | * AkCache::lookupStore() |
|---|
| | 127 | * |
|---|
| | 128 | * Calling it with: |
|---|
| | 129 | * |
|---|
| | 130 | * AkCache::lookupStore(true) |
|---|
| | 131 | * |
|---|
| | 132 | * will return the configured $cache_store |
|---|
| | 133 | * |
|---|
| | 134 | * @param mixed $options |
|---|
| | 135 | * @return mixed false if no cache could be configured or AkCache instance |
|---|
| | 136 | */ |
|---|
| | 137 | function &lookupStore($options = null) |
|---|
| | 138 | { |
|---|
| | 139 | static $cache_store; |
|---|
| | 140 | $false = false; |
|---|
| | 141 | if ($options === true && !empty($cache_store)) { |
|---|
| | 142 | return $cache_store; |
|---|
| | 143 | } else if (is_array($options) && |
|---|
| | 144 | isset($options['enabled']) && $options['enabled']==true && |
|---|
| | 145 | isset($options['handler']) && |
|---|
| | 146 | isset($options['handler']['type'])) { |
|---|
| | 147 | $type = $options['handler']['type']; |
|---|
| | 148 | $options = isset($options['handler']['options'])?$options['handler']['options']:array(); |
|---|
| | 149 | } else if (is_string($options) || is_int($options)) { |
|---|
| | 150 | $type = $options; |
|---|
| | 151 | $options = array(); |
|---|
| | 152 | } else { |
|---|
| | 153 | return $false; |
|---|
| | 154 | } |
|---|
| | 155 | $cache_store = new AkCache(); |
|---|
| | 156 | $cache_store->init($options,$type); |
|---|
| | 157 | if ($cache_store->cache_enabled) { |
|---|
| | 158 | return $cache_store; |
|---|
| | 159 | } |
|---|
| | 160 | return $false; |
|---|
| | 161 | } |
|---|
| | 162 | |
|---|
| | 163 | function expandCacheKey($key, $namespace = null) |
|---|
| | 164 | { |
|---|
| | 165 | $expanded_cache_key = $namespace != null? $namespace : ''; |
|---|
| | 166 | if (isset($_ENV['AK_CACHE_ID'])) { |
|---|
| | 167 | $expanded_cache_key .= DS . $_ENV['AK_CACHE_ID']; |
|---|
| | 168 | } else if (isset($_ENV['AK_APP_VERSION'])) { |
|---|
| | 169 | $expanded_cache_key .= DS . $_ENV['AK_APP_VERSION']; |
|---|
| | 170 | } |
|---|
| | 171 | |
|---|
| | 172 | if (is_object($key) && method_exists($key,'cacheKey')) { |
|---|
| | 173 | $expanded_cache_key .= DS . $key->cacheKey(); |
|---|
| | 174 | } else if (is_array($key)) { |
|---|
| | 175 | foreach ($key as $idx => $v) { |
|---|
| | 176 | $expanded_cache_key .= DS . $idx.'='.$v; |
|---|
| | 177 | } |
|---|
| | 178 | } else { |
|---|
| | 179 | $expanded_cache_key .= DS . $key; |
|---|
| | 180 | } |
|---|
| | 181 | $regex = '|'.DS.'+|'; |
|---|
| | 182 | $expanded_cache_key = preg_replace($regex,DS, $expanded_cache_key); |
|---|
| | 183 | $expanded_cache_key = rtrim($expanded_cache_key,DS); |
|---|
| | 184 | return $expanded_cache_key; |
|---|
| | 185 | } |
|---|