Changeset 886
- Timestamp:
- 07/17/08 05:47:13 (2 years ago)
- Files:
-
- branches/arnoschn/cache/lib/Ak.php (modified) (2 diffs)
- branches/arnoschn/cache/lib/AkActionController/AkCacheHandler.php (modified) (8 diffs)
- branches/arnoschn/cache/lib/AkCache/AkMemcache.php (modified) (3 diffs)
- branches/arnoschn/cache/lib/AkConverters/AkDoubleToString.php (added)
- branches/arnoschn/cache/lib/AkConverters/AkFloatToString.php (added)
- branches/arnoschn/cache/lib/AkConverters/AkIntegerToString.php (added)
- branches/arnoschn/cache/lib/AkConverters/AkStringToArray.php (added)
- branches/arnoschn/cache/lib/AkConverters/AkStringToDouble.php (added)
- branches/arnoschn/cache/lib/AkConverters/AkStringToFloat.php (added)
- branches/arnoschn/cache/lib/AkConverters/AkStringToInteger.php (added)
- branches/arnoschn/cache/lib/AkObject.php (modified) (2 diffs)
- branches/arnoschn/cache/test/unit/lib/Ak.php (modified) (1 diff)
- branches/arnoschn/cache/test/unit/lib/AkCache.php (modified) (1 diff)
- branches/arnoschn/cache/test/unit/lib/AkCache/AkMemcache.php (modified) (2 diffs)
- branches/arnoschn/cache/test/unit/lib/AkObject.php (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/arnoschn/cache/lib/Ak.php
r882 r886 1415 1415 $options = $args; 1416 1416 } 1417 1417 if ($options['from'] == $options['to']) { 1418 return $options['source']; 1419 } 1418 1420 $options['class_prefix'] = empty($options['class_prefix']) && empty($options['path']) ? 'Ak' : $options['class_prefix']; 1419 1421 $options['path'] = rtrim(empty($options['path']) ? AK_LIB_DIR.DS.'AkConverters' : $options['path'], DS."\t "); … … 1873 1875 return $return; 1874 1876 } 1877 1878 /** 1879 * 1880 * @param unknown_type $options 1881 * @param unknown_type $default_options 1882 * @param unknown_type $available_options 1883 * @param unknown_type $walk_keys 1884 * @return unknown 1885 */ 1886 function parseOptions($options = array(), $default_options = array(), $available_options = array(), $walk_keys=false) 1887 { 1888 $parsedOptions = array(); 1889 if ($walk_keys) { 1890 foreach ($options as $key=>$value) { 1891 if (!is_array($value)) { 1892 $parsedOptions[$value] = $default_options; 1893 } else { 1894 $parsedOptions[$key] = Ak::parseOptions($value, $default_options, $available_options); 1895 } 1896 } 1897 return $parsedOptions; 1898 } 1899 1900 $options = array_merge($default_options, $options); 1901 foreach($options as $key => $value) { 1902 if((is_array($available_options) && isset($available_options[$key])) || $available_options === true) { 1903 $parsedOptions[$key] = Ak::convert(gettype($value),$available_options===true?gettype($value):$available_options[$key],$value); 1904 } 1905 } 1906 return $parsedOptions; 1907 } 1875 1908 } 1876 1909 branches/arnoschn/cache/lib/AkActionController/AkCacheHandler.php
r852 r886 14 14 var $_controller; 15 15 16 /** Action Caching **/ 17 18 var $_actionCacheAvailableOptions = array('include_get_parameters'=>'array','cache_path'=>'string'); 19 var $_actionCacheDefaultOptions = array('include_get_parameters'=>array(),'cache_path'=>''); 16 20 17 21 /** … … 19 23 */ 20 24 21 25 var $_pageCacheAvailableOptions = array('include_get_parameters','array','headers'=>'array'); 26 var $_pageCacheDefaultOptions = array('include_get_parameters'=>array(),'headers'=>array('X-Cached-By'=>'Akelos')); 27 22 28 var $_lastCacheGroup; 23 29 … … 54 60 var $observe = array(); 55 61 62 var $_sweeperAvailableOptions = array('only'=>'array','except'=>'array'); 63 var $_sweeperDefaultOptions = array('only'=>array(),'except'=>array()); 64 56 65 var $_Sweepers = array(); 57 66 58 var $__availableObserveEvents = array('create','update','destroy','save');59 67 /** 60 68 * Reads configuration options from AkActionController and the configured … … 181 189 function _cacheSweeper($options) 182 190 { 183 if (!is_array($options)) { 184 $this->_initSweeper($options); 185 } else { 186 if (isset($options[0])) { 187 foreach ($options as $sweeper) { 188 $this->_initSweeper($sweeper); 189 } 190 } else { 191 foreach ($options as $sweeper => $params) { 192 if (is_int($sweeper)) { 193 $sweeper = $params; 194 $params = array(); 195 } 196 $this->_initSweeper($sweeper, $params); 197 } 198 } 199 191 $options = Ak::parseOptions($options, $this->_sweeperDefaultOptions, $this->_sweeperAvailableOptions, true); 192 193 foreach ($options as $sweeper => $params) { 194 if (is_int($sweeper)) { 195 $sweeper = $params; 196 $params = array(); 197 } 198 $this->_initSweeper($sweeper, $params); 200 199 } 201 200 } … … 203 202 function _initSweeper($sweeper, $params = array()) 204 203 { 205 $only = isset($params['only'])?Ak::toArray($params['only']):array(); 206 $except = isset($params['except'])?Ak::toArray($params['except']):array(); 207 208 if (!empty($only) && !in_array($this->_controller->getActionName(), $only)) return; 209 if (!empty($except) && !in_array($this->_controller->getActionName(), $except)) return; 204 $options = Ak::parseOptions($params, $this->_sweeperDefaultOptions, $this->_sweeperAvailableOptions); 205 206 if (!empty($only) && !in_array($this->_controller->getActionName(), $options['only'])) return; 207 if (!empty($except) && !in_array($this->_controller->getActionName(), $options['except'])) return; 210 208 211 209 $sweeper_class = AkInflector::classify($sweeper); … … 229 227 if (!$this->_perform_caching) return; 230 228 231 $this->_caches_page = is_array($options)?$options:Ak::toArray($options);229 $this->_caches_page = Ak::parseOptions($options, $this->_pageCacheDefaultOptions, $this->_pageCacheAvailableOptions, true); 232 230 $actionName = $this->_controller->getActionName(); 233 if (($hasOptions = isset($this->_caches_page[$actionName])) || 234 in_array($actionName, $this->_caches_page)) { 235 if ($hasOptions) { 236 $this->_include_get_parameters = isset($this->_caches_page[$actionName]['include_get_parameters'])? 237 is_array($this->_caches_page[$actionName]['include_get_parameters'])? 238 $this->_caches_page[$actionName]['include_get_parameters']: Ak::toArray($this->_caches_page[$actionName]['include_get_parameters']) 239 :array(); 240 $this->_additional_headers = isset($this->_caches_page[$actionName]['headers'])? 241 is_array($this->_caches_page[$actionName]['headers'])? 242 $this->_caches_page[$actionName]['headers']: Ak::toArray($this->_caches_page[$actionName]['headers']) 243 :array(); 244 } 231 if (isset($this->_caches_page[$actionName])) { 232 233 $this->_include_get_parameters = $this->_caches_page[$actionName]['include_get_parameters']; 234 $this->_additional_headers = $this->_caches_page[$actionName]['headers']; 235 245 236 $this->_controller->prependBeforeFilter(array(&$this,'beforePageCache')); 246 237 $this->_controller->appendAfterFilter(array(&$this,'afterPageCache')); … … 493 484 if (!$this->_perform_caching) return; 494 485 495 $this->_caches_action = is_array($options)?$options:Ak::toArray($options); 486 $this->_caches_action =Ak::parseOptions($options,$this->_actionCacheDefaultOptions,$this->_actionCacheAvailableOptions,true); 487 496 488 $actionName = $this->_controller->getActionName(); 497 if (($hasOptions = isset($this->_caches_action[$actionName])) || 498 in_array($actionName, $this->_caches_action)) { 499 if ($hasOptions) { 500 $this->_action_include_get_parameters = isset($this->_caches_action[$actionName]['include_get_parameters'])? 501 is_array($this->_caches_action[$actionName]['include_get_parameters'])? 502 $this->_caches_action[$actionName]['include_get_parameters']: Ak::toArray($this->_caches_action[$actionName]['include_get_parameters']) 503 :array(); 504 $path = isset($this->_caches_action[$actionName]['cache_path'])? 505 $this->_caches_action[$actionName]['cache_path']:null; 506 $parts = parse_url($path); 507 if (isset($parts['host'])) { 508 $this->_action_cache_host = $parts['host']; 509 $this->_action_cache_path = $parts['path']; 510 } else { 511 $this->_action_cache_path = $path; 512 } 513 } 489 490 if (isset($this->_caches_action[$actionName])) { 491 492 $this->_action_include_get_parameters = $this->_caches_action[$actionName]['include_get_parameters']; 493 $path = $this->_caches_action[$actionName]['cache_path']; 494 $parts = parse_url($path); 495 if (isset($parts['host'])) { 496 $this->_action_cache_host = $parts['host']; 497 $this->_action_cache_path = $parts['path']; 498 } else { 499 $this->_action_cache_path = $path; 500 } 501 514 502 if (!isset($this->_action_cache_host)) { 515 503 $this->_action_cache_host = $this->_controller->Request->getHost(); … … 527 515 if (is_array($options)) { 528 516 $path = $this->_pathFor($options); 529 } else if ($options == null ) {517 } else if ($options == null || empty($options)) { 530 518 $path = $this->_pathFor(); 531 519 } else { branches/arnoschn/cache/lib/AkCache/AkMemcache.php
r885 r886 4 4 class AkMemcache extends AkObject 5 5 { 6 6 7 var $_defaultOptions = array('servers'=>'localhost:11211'); 8 var $_availableOptions = array('servers'=>'array'); 9 7 10 /** 8 11 * @var MemCachedClient … … 25 28 var $_max_size = 1000000; 26 29 30 var $_servers = array(); 31 27 32 function init($options = array()) 28 33 { 29 $servers = isset($options['servers'])?$options['servers']:array(); 30 $servers = is_array($servers)?$servers:array($servers); 31 if (empty($servers)) { 34 $this->setOptions($options); 35 if (empty($this->_servers)) { 32 36 trigger_error('Need to provide at least 1 server',E_USER_ERROR); 33 37 return false; 34 38 } 35 $this->_memcache = new MemCachedClient($ servers /**array('127.0.0.1:11211')*/);39 $this->_memcache = new MemCachedClient($this->_servers /**array('127.0.0.1:11211')*/); 36 40 $ping = $this->_memcache->get('ping'); 37 41 if (!$ping) { … … 44 48 return true; 45 49 } 50 46 51 47 52 function _getNamespaceId($group) branches/arnoschn/cache/lib/AkObject.php
r855 r886 35 35 { 36 36 37 // ------ OPTION HANDLING ----------// 38 var $_defaultOptions = array(); 39 var $_availableOptions = array(); 37 40 38 41 … … 71 74 } 72 75 } 73 74 // }}} 76 77 // }}} 78 79 // ------ OPTION HANDLING ----------// 80 function setOptions($options = array(),$prefix='_') 81 { 82 $options = Ak::parseOptions($options,$this->_defaultOptions, $this->_availableOptions); 83 foreach($options as $key => $value) { 84 $property = $prefix.$key; 85 $this->$property = $value; 86 } 87 } 88 89 75 90 // {{{ toString() 76 91 branches/arnoschn/cache/test/unit/lib/Ak.php
r856 r886 119 119 $this->assertEqual($null, $storedValue3); 120 120 } 121 122 function test_parse_options() 123 { 124 $defaultOptions = array('test'=>1); 125 $availableOptions = array('test'=>'integer'); 126 $result = Ak::parseOptions(array('nada'=>1), $defaultOptions, $availableOptions); 127 $this->assertEqual($defaultOptions, $result); 128 } 129 130 function test_parse_options_convert_string_to_int() 131 { 132 $defaultOptions = array('test'=>1); 133 $availableOptions = array('test'=>'integer'); 134 $result = Ak::parseOptions(array('test'=>'1'), $defaultOptions, $availableOptions); 135 $this->assertEqual($defaultOptions, $result); 136 } 137 138 function test_parse_options_convert_int_to_string() 139 { 140 $defaultOptions = array('test'=>'1'); 141 $availableOptions = array('test'=>'string'); 142 $result = Ak::parseOptions(array('test'=>1), $defaultOptions, $availableOptions); 143 $this->assertEqual($defaultOptions, $result); 144 } 145 146 function test_parse_options_convert_string_to_float() 147 { 148 $defaultOptions = array('test'=>1.5); 149 $availableOptions = array('test'=>'float'); 150 $result = Ak::parseOptions(array('test'=>'1.5'), $defaultOptions, $availableOptions); 151 $this->assertEqual($defaultOptions, $result); 152 } 153 154 function test_parse_options_convert_double_to_string() 155 { 156 $defaultOptions = array('test'=>'1.5'); 157 $availableOptions = array('test'=>'string'); 158 $result = Ak::parseOptions(array('test'=>1.5), $defaultOptions, $availableOptions); 159 $this->assertEqual($defaultOptions, $result); 160 } 161 162 function test_parse_options_convert_string_to_array() 163 { 164 $defaultOptions = array('test'=>array(0,1)); 165 $availableOptions = array('test'=>'array'); 166 $result = Ak::parseOptions(array('test'=>'0,1'), $defaultOptions, $availableOptions); 167 $this->assertEqual($defaultOptions, $result); 168 } 169 170 function test_parse_options_convert_with_keys() 171 { 172 $defaultOptions = array('test'=>1); 173 $availableOptions = array('test'=>'integer'); 174 $result = Ak::parseOptions(array('key1'=>array('test'=>'1','nada'=>1),'key2'), $defaultOptions, $availableOptions,true); 175 $this->assertEqual($defaultOptions, $result['key1']); 176 $this->assertEqual($defaultOptions, $result['key2']); 177 } 121 178 } 179 ak_test('Test_Ak'); branches/arnoschn/cache/test/unit/lib/AkCache.php
r884 r886 93 93 94 94 //Cache Lite cache 95 $this->Cache->init( 1,1);95 $this->Cache->init(2,1); 96 96 $this->assertFalse(!$this->Cache->save($this->text_to_catch, $this->id, $this->group),'saving the cache (File based)'); 97 97 $this->Cache->init(1,1); branches/arnoschn/cache/test/unit/lib/AkCache/AkMemcache.php
r882 r886 18 18 { 19 19 $this->memcache=new AkMemcache(); 20 20 $this->memcache->_defaultOptions = array(); 21 21 $res = $this->memcache->init(array()); 22 22 $this->assertFalse($res); … … 167 167 168 168 169 ak_test('Test_AkMemcache' ,true);169 ak_test('Test_AkMemcache'); 170 170 ?>
