Apache Zeta Components Manual :: File Source for lru.php

Source for file lru.php

Documentation is available at lru.php

  1. <?php
  2. /**
  3.  * File containing the ezcCacheStackLruReplacementStrategy class.
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @package Cache
  23.  * @version //autogentag//
  24.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  25.  * @filesource
  26.  */
  27.  
  28. /**
  29.  * Least recently used replacement strategy.
  30.  *
  31.  * This replacement strategy will purge items first that have been used least
  32.  * recently. In case the {@link ezcCacheStackableStorage} this replacement
  33.  * strategy works on runs full, first all outdated items (which are older than
  34.  * TTL) will be purged. If this does not last to achieve the desired free rate,
  35.  * items will be purged that have not been stored or restored for the longest
  36.  * time, until the free rate is reached.
  37.  *
  38.  * This class is not intended to be used directly, but should be configured to
  39.  * be used by an {@link ezcCacheStack} instance. This can be achieved via
  40.  * {@link ezcCacheStackOptions}. The meta data class used by this class is
  41.  * {@link ezcCacheStackLruMetaData}.
  42.  *
  43.  * For more information on LRU see {@see 
  44.  * http://en.wikipedia.org/wiki/Cache_algorithms}.
  45.  *
  46.  * @package Cache
  47.  * @version //autogentag//
  48.  */
  49. class ezcCacheStackLruReplacementStrategy extends ezcCacheStackBaseReplacementStrategy
  50. {
  51.     /**
  52.      * Stores the given $itemData in the given storage.
  53.      *
  54.      * This method stores the given $itemData under the given $itemId and
  55.      * assigns the given $itemAttributes to it in the {@link }
  56.      * ezcCacheStackableStorage} configured in $conf. The
  57.      * storing results in an update of $metaData, reflecting that the item with
  58.      * $itemId was recently used.
  59.      *
  60.      * In case the number of items in the storage exceeds $conf->itemLimit,
  61.      * items will be deleted from the storage. First all outdated items will be
  62.      * removed using {@link ezcCacheStackableStorage::purge()}. If this does
  63.      * not free the desired $conf->freeRate fraction of $conf->itemLimit, those
  64.      * items that have been used least recently will be deleted. The changes of
  65.      * freeing items are recorded in $metaData.
  66.      *
  67.      * @param ezcCacheStackStorageConfiguration $conf 
  68.      * @param ezcCacheStackMetaData $metaData 
  69.      * @param string $itemId 
  70.      * @param mixed $itemData 
  71.      * @param array(string=>string) $itemAttributes 
  72.      *
  73.      * @see ezcCacheStackReplacementStrategy::store()
  74.      *
  75.      * @throws ezcCacheInvalidMetaDataException
  76.      *          if the given $metaData is not an instance of {@link }
  77.      *          ezcCacheStackLruMetaData}.
  78.      */
  79.     public static function store(
  80.         ezcCacheStackStorageConfiguration $conf,
  81.         ezcCacheStackMetaData $metaData,
  82.         $itemId,
  83.         $itemData,
  84.         $itemAttributes array()
  85.     )
  86.     {
  87.         self::checkMetaData$metaData );
  88.         return parent::store$conf$metaData$itemId$itemData$itemAttributes );
  89.     }
  90.  
  91.     /**
  92.      * Restores the data with the given $itemId from the storage configured in $conf.
  93.      *
  94.      * This method restores the item data identified by $itemId and optionally
  95.      * $itemAttributes from the {@link ezcCacheStackableStorage} given in
  96.      * $conf using {@link ezcCacheStackableStorage::restore()}. The result of
  97.      * this action is returned by the method. This means, the desired item data
  98.      * is returned on success, false is returned if the data is not available.
  99.      *
  100.      * A successful restore is recorded in $metaData as a "recent usage", with
  101.      * updating the last usage timestamp of $itemId to the current time. A
  102.      * restore failure results in a removal of $itemId.
  103.      *
  104.      * @param ezcCacheStackStorageConfiguration $conf 
  105.      * @param ezcCacheStackMetaData $metaData 
  106.      * @param string $itemId 
  107.      * @param array(string=>string) $itemAttributes 
  108.      * @param bool $search 
  109.      *
  110.      * @see ezcCacheStackReplacementStrategy::restore()
  111.      *
  112.      * @return mixed Restored data or false.
  113.      *
  114.      * @throws ezcCacheInvalidMetaDataException
  115.      *          if the given $metaData is not an instance of {@link }
  116.      *          ezcCacheStackLruMetaData}.
  117.      */
  118.     public static function restore(
  119.         ezcCacheStackStorageConfiguration $conf,
  120.         ezcCacheStackMetaData $metaData,
  121.         $itemId,
  122.         $itemAttributes array(),
  123.         $search false
  124.     )
  125.     {
  126.         self::checkMetaData$metaData );
  127.         return parent::restore$conf$metaData$itemId$itemAttributes$search );
  128.     }
  129.  
  130.     /**
  131.      * Deletes the data with the given $itemId from the given $storage.
  132.      *
  133.      * Deletes the desired item with $itemId and optionally $itemAttributes
  134.      * from the {@link ezcCacheStackableStorage} configured in $conf using. The
  135.      * item IDs returned by this call are updated in $metaData, that they are
  136.      * no longer stored in the $storage.
  137.      *
  138.      * @param ezcCacheStackStorageConfiguration $conf 
  139.      * @param ezcCacheStackMetaData $metaData 
  140.      * @param string $itemId 
  141.      * @param array(string=>string) $itemAttributes 
  142.      * @param bool $search 
  143.      *
  144.      * @see ezcCacheStackReplacementStrategy::delete()
  145.      *
  146.      * @return array(string) Deleted item IDs.
  147.      *
  148.      * @throws ezcCacheInvalidMetaDataException
  149.      *          if the given $metaData is not an instance of {@link }
  150.      *          ezcCacheStackLruMetaData}.
  151.      */
  152.     public static function delete(
  153.         ezcCacheStackStorageConfiguration $conf,
  154.         ezcCacheStackMetaData $metaData,
  155.         $itemId,
  156.         $itemAttributes array(),
  157.         $search false
  158.     )
  159.     {
  160.         self::checkMetaData$metaData );
  161.         return parent::delete$conf$metaData$itemId$itemAttributes$search );
  162.     }
  163.  
  164.     /**
  165.      * Returns a fresh meta data instance.
  166.      *
  167.      * Returns a freshly created instance of {@link ezcCacheStackLruMetaData}.
  168.      * 
  169.      * @return ezcCacheStackLruMetaData 
  170.      */
  171.     public static function createMetaData()
  172.     {
  173.         return new ezcCacheStackLruMetaData();
  174.     }
  175.  
  176.     /**
  177.      * Checks if the given meta data is processable.
  178.      *
  179.      * Throws an exception if the given meta data is not processable.
  180.      * 
  181.      * @param ezcCacheStackMetaData $metaData 
  182.      *
  183.      * @throws ezcCacheInvalidMetaDataException
  184.      *          if the given $metaData is not an instance of {@link }
  185.      *          ezcCacheStackLruMetaData}.
  186.      *
  187.      * @access private
  188.      */
  189.     protected static function checkMetaDataezcCacheStackMetaData $metaData )
  190.     {
  191.         if !$metaData instanceof ezcCacheStackLruMetaData ) )
  192.         {
  193.             throw new ezcCacheInvalidMetaDataException(
  194.                 $metaData,
  195.                 'ezcCacheStackLruMetaData'
  196.             );
  197.         }
  198.     }
  199. }
  200.  
  201. ?>
Documentation generated by phpDocumentor 1.4.3