Advertisement
Guest User

Untitled

a guest
May 18th, 2012
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 24.24 KB | None | 0 0
  1. Index: Apc.php
  2. ===================================================================
  3. --- Apc.php (revision 24811)
  4. +++ Apc.php (working copy)
  5. @@ -41,12 +41,30 @@
  6.  class Zend_Cache_Backend_Apc extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
  7.  {
  8.      /**
  9. -     * Log message
  10. +     * Available options
  11. +     *
  12. +     * =====> (int) metatadatas_array_max_size :
  13. +     * - max size for the metadatas array (don't change this value unless you
  14. +     *   know what you are doing)
  15. +     * =====> (string) apc_id_prefix :
  16. +     * - prefix for the values stored in APC cache.
  17. +     *
  18. +     * @var array available options
  19.       */
  20. -    const TAGS_UNSUPPORTED_BY_CLEAN_OF_APC_BACKEND = 'Zend_Cache_Backend_Apc::clean() : tags are unsupported by the Apc backend';
  21. -    const TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND =  'Zend_Cache_Backend_Apc::save() : tags are unsupported by the Apc backend';
  22. +    protected $_options = array(
  23. +        'metadatas_array_max_size' => 100,
  24. +        'apc_id_prefix' => ''
  25. +    );
  26.  
  27.      /**
  28. +     * Array of metadatas (each item is an associative array)
  29. +     *
  30. +     * @var array
  31. +     */
  32. +    protected $_metadatasArray = array();
  33. +
  34. +
  35. +    /**
  36.       * Constructor
  37.       *
  38.       * @param  array $options associative array of options
  39. @@ -59,6 +77,9 @@
  40.              Zend_Cache::throwException('The apc extension must be loaded for using this backend !');
  41.          }
  42.          parent::__construct($options);
  43. +        if ($this->_options['metadatas_array_max_size'] < 10) {
  44. +            Zend_Cache::throwException('Invalid metadatas_array_max_size, must be > 10');
  45. +        }
  46.      }
  47.  
  48.      /**
  49. @@ -72,11 +93,7 @@
  50.       */
  51.      public function load($id, $doNotTestCacheValidity = false)
  52.      {
  53. -        $tmp = apc_fetch($id);
  54. -        if (is_array($tmp)) {
  55. -            return $tmp[0];
  56. -        }
  57. -        return false;
  58. +        return apc_fetch($this->_id($id));
  59.      }
  60.  
  61.      /**
  62. @@ -87,9 +104,10 @@
  63.       */
  64.      public function test($id)
  65.      {
  66. -        $tmp = apc_fetch($id);
  67. -        if (is_array($tmp)) {
  68. -            return $tmp[1];
  69. +        $id = $this->_id($id);
  70. +        $metadatas = $this->_getMetadatas($id);
  71. +        if ($metadatas !== false && apc_exists($id)) {
  72. +            return $metadatas['mtime'];
  73.          }
  74.          return false;
  75.      }
  76. @@ -108,11 +126,21 @@
  77.       */
  78.      public function save($data, $id, $tags = array(), $specificLifetime = false)
  79.      {
  80. +        $id = $this->_id($id);
  81.          $lifetime = $this->getLifetime($specificLifetime);
  82. -        $result = apc_store($id, array($data, time(), $lifetime), $lifetime);
  83. -        if (count($tags) > 0) {
  84. -            $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
  85. +        $mtime = time();
  86. +        $metadatas = array(
  87. +            'expire' => $mtime + $lifetime,
  88. +            'tags' => $tags,
  89. +            'mtime' => $mtime,
  90. +            'ttl' => $lifetime
  91. +        );
  92. +        $result = $this->_setMetadatas($id, $metadatas);
  93. +        if (!$result) {
  94. +            $this->_log('Zend_Cache_Backend_Apc::save() / error on saving metadata');
  95. +            return false;
  96.          }
  97. +        $result = apc_store($id, $data, $lifetime);
  98.          return $result;
  99.      }
  100.  
  101. @@ -124,87 +152,54 @@
  102.       */
  103.      public function remove($id)
  104.      {
  105. -        return apc_delete($id);
  106. +        $id = $this->_id($id);
  107. +        $boolRemove   = $this->_remove($id);
  108. +        $boolMetadata = $this->_delMetadatas($id);
  109. +        return $boolMetadata && $boolRemove;
  110.      }
  111.  
  112.      /**
  113.       * Clean some cache records
  114.       *
  115.       * Available modes are :
  116. -     * 'all' (default)  => remove all cache entries ($tags is not used)
  117. -     * 'old'            => unsupported
  118. -     * 'matchingTag'    => unsupported
  119. -     * 'notMatchingTag' => unsupported
  120. -     * 'matchingAnyTag' => unsupported
  121.       *
  122. +     * Zend_Cache::CLEANING_MODE_ALL (default)    => remove all cache entries ($tags is not used)
  123. +     * Zend_Cache::CLEANING_MODE_OLD              => remove too old cache entries ($tags is not used)
  124. +     * Zend_Cache::CLEANING_MODE_MATCHING_TAG     => remove cache entries matching all given tags
  125. +     *                                               ($tags can be an array of strings or a single string)
  126. +     * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
  127. +     *                                               ($tags can be an array of strings or a single string)
  128. +     * Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags
  129. +     *                                               ($tags can be an array of strings or a single string)
  130. +     *
  131.       * @param  string $mode clean mode
  132.       * @param  array  $tags array of tags
  133. -     * @throws Zend_Cache_Exception
  134.       * @return boolean true if no problem
  135.       */
  136.      public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
  137.      {
  138. -        switch ($mode) {
  139. -            case Zend_Cache::CLEANING_MODE_ALL:
  140. -                return apc_clear_cache('user');
  141. -                break;
  142. -            case Zend_Cache::CLEANING_MODE_OLD:
  143. -                $this->_log("Zend_Cache_Backend_Apc::clean() : CLEANING_MODE_OLD is unsupported by the Apc backend");
  144. -                break;
  145. -            case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
  146. -            case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
  147. -            case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
  148. -                $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_APC_BACKEND);
  149. -                break;
  150. -            default:
  151. -                Zend_Cache::throwException('Invalid mode for clean() method');
  152. -                break;
  153. -        }
  154. +        // We use this protected method to hide the recursive stuff
  155. +        return $this->_clean($mode, $tags);
  156.      }
  157.  
  158.      /**
  159. -     * Return true if the automatic cleaning is available for the backend
  160. +     * Return an array of stored cache ids
  161.       *
  162. -     * DEPRECATED : use getCapabilities() instead
  163. -     *
  164. -     * @deprecated
  165. -     * @return boolean
  166. +     * @return array array of stored cache ids (string)
  167.       */
  168. -    public function isAutomaticCleaningAvailable()
  169. +    public function getIds()
  170.      {
  171. -        return false;
  172. +        return $this->_get('ids', array());
  173.      }
  174.  
  175.      /**
  176. -     * Return the filling percentage of the backend storage
  177. -     *
  178. -     * @throws Zend_Cache_Exception
  179. -     * @return int integer between 0 and 100
  180. -     */
  181. -    public function getFillingPercentage()
  182. -    {
  183. -        $mem = apc_sma_info(true);
  184. -        $memSize    = $mem['num_seg'] * $mem['seg_size'];
  185. -        $memAvailable= $mem['avail_mem'];
  186. -        $memUsed = $memSize - $memAvailable;
  187. -        if ($memSize == 0) {
  188. -            Zend_Cache::throwException('can\'t get apc memory size');
  189. -        }
  190. -        if ($memUsed > $memSize) {
  191. -            return 100;
  192. -        }
  193. -        return ((int) (100. * ($memUsed / $memSize)));
  194. -    }
  195. -
  196. -    /**
  197.       * Return an array of stored tags
  198.       *
  199.       * @return array array of stored tags (string)
  200.       */
  201.      public function getTags()
  202.      {
  203. -        $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
  204. -        return array();
  205. +        return $this->_get('tags', array());
  206.      }
  207.  
  208.      /**
  209. @@ -217,8 +212,7 @@
  210.       */
  211.      public function getIdsMatchingTags($tags = array())
  212.      {
  213. -        $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
  214. -        return array();
  215. +        return $this->_get('matching', $tags);
  216.      }
  217.  
  218.      /**
  219. @@ -231,8 +225,7 @@
  220.       */
  221.      public function getIdsNotMatchingTags($tags = array())
  222.      {
  223. -        $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
  224. -        return array();
  225. +        return $this->_get('notMatching', $tags);
  226.      }
  227.  
  228.      /**
  229. @@ -245,24 +238,28 @@
  230.       */
  231.      public function getIdsMatchingAnyTags($tags = array())
  232.      {
  233. -        $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_APC_BACKEND);
  234. -        return array();
  235. +        return $this->_get('matchingAny', $tags);
  236.      }
  237.  
  238.      /**
  239. -     * Return an array of stored cache ids
  240. +     * Return the filling percentage of the backend storage
  241.       *
  242. -     * @return array array of stored cache ids (string)
  243. +     * @throws Zend_Cache_Exception
  244. +     * @return int integer between 0 and 100
  245.       */
  246. -    public function getIds()
  247. +    public function getFillingPercentage()
  248.      {
  249. -        $res = array();
  250. -        $array = apc_cache_info('user', false);
  251. -        $records = $array['cache_list'];
  252. -        foreach ($records as $record) {
  253. -            $res[] = $record['info'];
  254. +        $mem = apc_sma_info(true);
  255. +        $memSize    = $mem['num_seg'] * $mem['seg_size'];
  256. +        $memAvailable= $mem['avail_mem'];
  257. +        $memUsed = $memSize - $memAvailable;
  258. +        if ($memSize == 0) {
  259. +            Zend_Cache::throwException('can\'t get apc memory size');
  260.          }
  261. -        return $res;
  262. +        if ($memUsed > $memSize) {
  263. +            return 100;
  264. +        }
  265. +        return ((int) (100. * ($memUsed / $memSize)));
  266.      }
  267.  
  268.      /**
  269. @@ -278,23 +275,12 @@
  270.       */
  271.      public function getMetadatas($id)
  272.      {
  273. -        $tmp = apc_fetch($id);
  274. -        if (is_array($tmp)) {
  275. -            $data = $tmp[0];
  276. -            $mtime = $tmp[1];
  277. -            if (!isset($tmp[2])) {
  278. -                // because this record is only with 1.7 release
  279. -                // if old cache records are still there...
  280. -                return false;
  281. -            }
  282. -            $lifetime = $tmp[2];
  283. -            return array(
  284. -                'expire' => $mtime + $lifetime,
  285. -                'tags' => array(),
  286. -                'mtime' => $mtime
  287. -            );
  288. +        $id = $this->_id($id);
  289. +        $metadatas = $this->_getMetadatas($id);
  290. +        if (!$metadatas) {
  291. +            return false;
  292.          }
  293. -        return false;
  294. +        return $metadatas;
  295.      }
  296.  
  297.      /**
  298. @@ -306,22 +292,17 @@
  299.       */
  300.      public function touch($id, $extraLifetime)
  301.      {
  302. -        $tmp = apc_fetch($id);
  303. -        if (is_array($tmp)) {
  304. -            $data = $tmp[0];
  305. -            $mtime = $tmp[1];
  306. -            if (!isset($tmp[2])) {
  307. -                // because this record is only with 1.7 release
  308. -                // if old cache records are still there...
  309. +        $id = $this->_id($id);
  310. +        $metadatas = $this->_getMetadatas($id);
  311. +        if (false !== $tmp = apc_fetch($id)) {
  312. +            $metadatas['ttl'] = $metadatas['ttl'] - (time() - $metadatas['mtime']) + $extraLifetime;
  313. +            $metadatas['mtime'] = time();
  314. +            $metadatas['expire'] = $metadatas['mtime'] + $metadatas['ttl'];
  315. +            if ($metadatas['ttl'] <=0) {
  316.                  return false;
  317.              }
  318. -            $lifetime = $tmp[2];
  319. -            $newLifetime = $lifetime - (time() - $mtime) + $extraLifetime;
  320. -            if ($newLifetime <=0) {
  321. -                return false;
  322. -            }
  323. -            apc_store($id, array($data, time(), $newLifetime), $newLifetime);
  324. -            return true;
  325. +            $this->_setMetadatas($id, $metadatas);
  326. +            return apc_store($id, $tmp, $metadatas['ttl']);
  327.          }
  328.          return false;
  329.      }
  330. @@ -344,7 +325,7 @@
  331.      {
  332.          return array(
  333.              'automatic_cleaning' => false,
  334. -            'tags' => false,
  335. +            'tags' => true,
  336.              'expired_read' => false,
  337.              'priority' => false,
  338.              'infinite_lifetime' => false,
  339. @@ -352,4 +333,365 @@
  340.          );
  341.      }
  342.  
  343. +    /**
  344. +     * Get a metadatas record
  345. +     *
  346. +     * @param  string $id  Cache id
  347. +     * @return array|false Associative array of metadatas
  348. +     */
  349. +    protected function _getMetadatas($id)
  350. +    {
  351. +        if (isset($this->_metadatasArray[$id])) {
  352. +            return $this->_metadatasArray[$id];
  353. +        } else {
  354. +            $metadatas = $this->_loadMetadatas($id);
  355. +            if (!$metadatas) {
  356. +                return false;
  357. +            }
  358. +            $this->_setMetadatas($id, $metadatas, false);
  359. +            return $metadatas;
  360. +        }
  361. +    }
  362. +
  363. +    /**
  364. +     * Set a metadatas record
  365. +     *
  366. +     * @param  string $id        Cache id
  367. +     * @param  array  $metadatas Associative array of metadatas
  368. +     * @param  boolean $save     optional pass false to disable saving to cache
  369. +     * @return boolean True if no problem
  370. +     */
  371. +    protected function _setMetadatas($id, $metadatas, $save = true)
  372. +    {
  373. +        if (count($this->_metadatasArray) >= $this->_options['metadatas_array_max_size']) {
  374. +            $n = (int) ($this->_options['metadatas_array_max_size'] / 10);
  375. +            $this->_metadatasArray = array_slice($this->_metadatasArray, $n);
  376. +        }
  377. +        if ($save) {
  378. +            $result = $this->_saveMetadatas($id, $metadatas);
  379. +            if (!$result) {
  380. +                return false;
  381. +            }
  382. +        }
  383. +        $this->_metadatasArray[$id] = $metadatas;
  384. +        return true;
  385. +    }
  386. +
  387. +    /**
  388. +     * Drop a metadata record
  389. +     *
  390. +     * @param  string $id Cache id
  391. +     * @return boolean True if no problem
  392. +     */
  393. +    protected function _delMetadatas($id)
  394. +    {
  395. +        if (isset($this->_metadatasArray[$id])) {
  396. +            unset($this->_metadatasArray[$id]);
  397. +        }
  398. +        $metas = $this->_loadMetadatasStore();
  399. +        if (isset($metas[$id])) {
  400. +            unset($metas[$id]);
  401. +        }
  402. +        $result = $this->_saveMetadatasStore($metas);
  403. +        return $result;
  404. +    }
  405. +
  406. +    /**
  407. +     * Load metadatas from cache
  408. +     *
  409. +     * @param  string $id Cache id
  410. +     * @return array|false Metadatas associative array
  411. +     */
  412. +    protected function _loadMetadatas($id)
  413. +    {
  414. +        $metas = $this->_loadMetadatasStore();
  415. +        if ($metas === false) {
  416. +            return false;
  417. +        }
  418. +
  419. +        if (!isset($metas[$id]) || empty($metas[$id])) {
  420. +            //    Metadatas for this id not available, remove it.
  421. +            $this->_remove($id);
  422. +            return false;
  423. +        }
  424. +        return $metas[$id];
  425. +    }
  426. +
  427. +    /**
  428. +     * Save metadatas to cache
  429. +     *
  430. +     * @param  string $id        Cache id
  431. +     * @param  array  $metadatas Associative array
  432. +     * @return boolean True if no problem
  433. +     */
  434. +    protected function _saveMetadatas($id, $metadatas)
  435. +    {
  436. +        $metas = $this->_loadMetadatasStore();
  437. +        if ($metas === false) {
  438. +            return false;
  439. +        }
  440. +        $metas[$id] = $metadatas;
  441. +        $result = $this->_saveMetadatasStore($metas);
  442. +        if (!$result) {
  443. +            return false;
  444. +        }
  445. +        return true;
  446. +    }
  447. +
  448. +    /**
  449. +     * Load the whole metadatas from cache
  450. +     *
  451. +     * @return array|false Metadatas associative array
  452. +     */
  453. +    protected function _loadMetadatasStore()
  454. +    {
  455. +        $result = apc_fetch($this->_metadatasId());
  456. +        if ($result === false) {
  457. +            //    Metadatas not available, clean the whole cache. It's useless without it anyways.
  458. +            $this->_clean(Zend_Cache::CLEANING_MODE_ALL, array(), true);
  459. +            //    Return an emty set to avoid failing the query. (Will fail anyways at the write)
  460. +            return array();
  461. +        }
  462. +        return @unserialize($result);
  463. +    }
  464. +    /**
  465. +     * Save the whole metadatas to cache
  466. +     *
  467. +     * @param  array  $metadatas Associative array
  468. +     * @return boolean True if no problem
  469. +     */
  470. +    protected function _saveMetadatasStore($metadatas)
  471. +    {
  472. +        $result = apc_store($this->_metadatasId(), serialize($metadatas), 0);
  473. +        if ($result === false) {
  474. +            // Failed to save the metadatas, clean the cache.
  475. +            $this->_clean(Zend_Cache::CLEANING_MODE_ALL, array(), true);
  476. +            return false;
  477. +        }
  478. +        return true;
  479. +    }
  480. +
  481. +    /**
  482. +     * Make and return the key for metadatas
  483. +     *
  484. +     * @return string Metadatas cache id
  485. +     */
  486. +    protected function _metadatasId()
  487. +    {
  488. +        return 'internal-metadatas---' . $this->_id('');
  489. +    }
  490. +
  491. +    /**
  492. +     * Remove a key from cache
  493. +     *
  494. +     * @param  string $id Cache id
  495. +     * @return boolean True if ok
  496. +     */
  497. +    protected function _remove($id)
  498. +    {
  499. +        if (!apc_delete($id)) {
  500. +            return false;
  501. +        }
  502. +        return true;
  503. +    }
  504. +
  505. +    /**
  506. +     * Clean some cache records (protected method used for recursive stuff)
  507. +     *
  508. +     * Available modes are :
  509. +     * Zend_Cache::CLEANING_MODE_ALL (default)    => remove all cache entries ($tags is not used)
  510. +     * Zend_Cache::CLEANING_MODE_OLD              => unsupported
  511. +     * Zend_Cache::CLEANING_MODE_MATCHING_TAG     => remove cache entries matching all given tags
  512. +     *                                               ($tags can be an array of strings or a single string)
  513. +     * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
  514. +     *                                               ($tags can be an array of strings or a single string)
  515. +     * Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags
  516. +     *                                               ($tags can be an array of strings or a single string)
  517. +     *
  518. +     * @param  string $mode Clean mode
  519. +     * @param  array  $tags Array of tags
  520. +     * @param  boolean $blindClean If true, don't load metadatas, juste clean.
  521. +     * @throws Zend_Cache_Exception
  522. +     * @return boolean True if no problem
  523. +     */
  524. +    protected function _clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array(), $blindClean = false)
  525. +    {
  526. +        if ($mode === Zend_Cache::CLEANING_MODE_OLD) {
  527. +            $this->_log("Zend_Cache_Backend_Apc::clean() : CLEANING_MODE_OLD is unsupported by the Apc backend");
  528. +            return false;
  529. +        }
  530. +        $result = true;
  531. +        if (!$blindClean) {
  532. +            $metas = $this->_loadMetadatasStore();
  533. +            if ($metas === false) {
  534. +                //    Unable to read metadatas, clean the cache.
  535. +                $mode = Zend_Cache::CLEANING_MODE_ALL;
  536. +                $metas = array();
  537. +            }
  538. +        } else {
  539. +            $metas = array();
  540. +        }
  541. +        //    We forge a new metadatas store to prevent metadatas from fallen ids to stay on cache.
  542. +        $newMetas = array();
  543. +        $cleanList = array();
  544. +        //    List all cache entry for our cache. We are good neighbours, we don't touch or clean other caches.
  545. +        $iterator = new APCIterator('user', '/^' . $this->_options['apc_id_prefix'] . '/') ;
  546. +        foreach ($iterator as $entity) {
  547. +            $id = $entity['key'];
  548. +            if (!isset($metas[$id])) {
  549. +                //    No metadatas for this id, purge it from the cache.
  550. +                $cleanList[] = $id;
  551. +                continue;
  552. +            }
  553. +            $metadatas = $metas[$id];
  554. +            switch ($mode) {
  555. +                case Zend_Cache::CLEANING_MODE_ALL:
  556. +                    $cleanList[] = $id;
  557. +                break;
  558. +                case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
  559. +                    $matching = true;
  560. +                    foreach ($tags as $tag) {
  561. +                        if (!in_array($tag, $metadatas['tags'])) {
  562. +                            $matching = false;
  563. +                            break;
  564. +                        }
  565. +                    }
  566. +                    if ($matching) {
  567. +                        $cleanList[] = $id;;
  568. +                    } else {
  569. +                        $newMetas[] = $metadatas;
  570. +                    }
  571. +                break;
  572. +                case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
  573. +                    $matching = false;
  574. +                    foreach ($tags as $tag) {
  575. +                        if (in_array($tag, $metadatas['tags'])) {
  576. +                            $matching = true;
  577. +                            break;
  578. +                        }
  579. +                    }
  580. +                    if (!$matching) {
  581. +                        $cleanList[] = $id;;
  582. +                    } else {
  583. +                        $newMetas[] = $metadatas;
  584. +                    }
  585. +                break;
  586. +                case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
  587. +                    $matching = false;
  588. +                    foreach ($tags as $tag) {
  589. +                        if (in_array($tag, $metadatas['tags'])) {
  590. +                            $matching = true;
  591. +                            break;
  592. +                        }
  593. +                    }
  594. +                    if ($matching) {
  595. +                        $cleanList[] = $id;;
  596. +                    } else {
  597. +                        $newMetas[] = $metadatas;
  598. +                    }
  599. +                break;
  600. +                default:
  601. +                    Zend_Cache::throwException('Invalid mode for clean() method');
  602. +                break;
  603. +            }
  604. +        }
  605. +        apc_delete($cleanList);
  606. +        $result = $result && $this->_saveMetadatasStore($newMetas);
  607. +        return $result;
  608. +    }
  609. +
  610. +    /**
  611. +     * Get some cache records
  612. +     *
  613. +     * Available modes are :
  614. +     * 'ids'            =>
  615. +     * 'tags'            =>
  616. +     * 'matching'        =>
  617. +     * 'notMatching'    =>
  618. +     * 'matchingAny'    =>
  619. +     *
  620. +     * @param  string $dir  Directory to clean
  621. +     * @param  string $mode Clean mode
  622. +     * @param  array  $tags Array of tags
  623. +     * @throws Zend_Cache_Exception
  624. +     * @return boolean True if no problem
  625. +     */
  626. +    protected function _get($mode, $tags = array())
  627. +    {
  628. +        $result = array();
  629. +        $metas = $this->_loadMetadatasStore();
  630. +        foreach ($metas as $id => $metadatas) {
  631. +            switch ($mode) {
  632. +                case 'ids':
  633. +                    $result[] = $id;
  634. +                break;
  635. +                case 'tags':
  636. +                    $result = array_unique(array_merge($result, $metadatas['tags']));
  637. +                break;
  638. +                case 'matching':
  639. +                    $matching = true;
  640. +                    foreach ($tags as $tag) {
  641. +                        if (!in_array($tag, $metadatas['tags'])) {
  642. +                            $matching = false;
  643. +                            break;
  644. +                        }
  645. +                    }
  646. +                    if ($matching) {
  647. +                        $result[] = $id;
  648. +                    }
  649. +                break;
  650. +                case 'notMatching':
  651. +                    $matching = false;
  652. +                    foreach ($tags as $tag) {
  653. +                        if (in_array($tag, $metadatas['tags'])) {
  654. +                            $matching = true;
  655. +                            break;
  656. +                        }
  657. +                    }
  658. +                    if (!$matching) {
  659. +                        $result[] = $id;
  660. +                    }
  661. +                break;
  662. +                case 'matchingAny':
  663. +                    $matching = false;
  664. +                    foreach ($tags as $tag) {
  665. +                        if (in_array($tag, $metadatas['tags'])) {
  666. +                            $matching = true;
  667. +                            break;
  668. +                        }
  669. +                    }
  670. +                    if ($matching) {
  671. +                        $result[] = $id;
  672. +                    }
  673. +                break;
  674. +                default:
  675. +                    Zend_Cache::throwException('Invalid mode for _get() method');
  676. +                break;
  677. +            }
  678. +        }
  679. +        $result = array_unique($result);
  680. +        if ($mode !== 'tags' && strlen($this->_options['apc_id_prefix'])) {
  681. +            // Cleaning prefixes from ids
  682. +            foreach ($result as &$res) {
  683. +                $res = substr($res, strlen($this->_options['apc_id_prefix']));
  684. +            }
  685. +        }
  686. +        return $result;
  687. +    }
  688. +
  689. +    /**
  690. +     * Make and return a cache id
  691. +     *
  692. +     * Checks 'apc_id_prefix' and returns new id with prefix or simply the id if null
  693. +     *
  694. +     * @param  string $id Cache id
  695. +     * @return string Cache id (with or without prefix)
  696. +     */
  697. +    protected function _id($id)
  698. +    {
  699. +        if (($id !== null) && isset($this->_options['apc_id_prefix'])) {
  700. +            return $this->_options['apc_id_prefix'] . $id; // return with prefix
  701. +        }
  702. +        return $id; // no prefix, just return the $id passed
  703. +    }
  704.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement