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

Source for file lfu.php

Documentation is available at lfu.php

  1. <?php
  2. /**
  3.  * File containing the ezcCacheStackLfuReplacementStrategy.
  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 frequently used replacement strategy.
  30.  *
  31.  * This replacement strategy will purge items first that have been used least
  32.  * frequently. 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 been stored or restored least often.
  36.  *
  37.  * This class is not intended to be used directly, but should be configured to
  38.  * be used by an {@link ezcCacheStack} instance. This can be achieved via
  39.  * {@link ezcCacheStackOptions}. The meta data class used by this class is
  40.  * {@link ezcCacheStackLfuMetaData}.
  41.  *
  42.  * For more information on LFU see {@see 
  43.  * http://en.wikipedia.org/wiki/Cache_algorithms}.
  44.  *
  45.  * @package Cache
  46.  * @version //autogentag//
  47.  */
  48. class ezcCacheStackLfuReplacementStrategy extends ezcCacheStackBaseReplacementStrategy
  49. {
  50.     /**
  51.      * Stores the given $itemData in the given storage.
  52.      *
  53.      * This method stores the given $itemData under the given $itemId and
  54.      * assigns the given $itemAttributes to it in the {@link }
  55.      * ezcCacheStackableStorage} configured in $conf. The
  56.      * storing results in an update of $metaData, reflecting that the item with
  57.      * $itemId was used by raising its use frequence.
  58.      *
  59.      * In case the number of items in the storage exceeds $conf->itemLimit,
  60.      * items will be deleted from the storage. First all outdated items will be
  61.      * removed using {@link ezcCacheStackableStorage::purge()}. If this does
  62.      * not free the desired $conf->freeRate fraction of $conf->itemLimit, those
  63.      * items that have been used least frequently will be deleted. The changes of
  64.      * freeing items are recorded in $metaData.
  65.      *
  66.      * @param ezcCacheStackStorageConfiguration $conf 
  67.      * @param ezcCacheStackMetaData $metaData 
  68.      * @param string $itemId 
  69.      * @param mixed $itemData 
  70.      * @param array(string=>string) $itemAttributes 
  71.      *
  72.      * @see ezcCacheStackReplacementStrategy::store()
  73.      *
  74.      * @throws ezcCacheInvalidMetaDataException
  75.      *          if the given $metaData is not an instance of {@link }
  76.      *          ezcCacheStackLfuMetaData}.
  77.      */
  78.     public static function store(
  79.         ezcCacheStackStorageConfiguration $conf,
  80.         ezcCacheStackMetaData $metaData,
  81.         $itemId,
  82.         $itemData,
  83.         $itemAttributes array()
  84.     )
  85.     {
  86.         self::checkMetaData$metaData );
  87.         return parent::store$conf$metaData$itemId$itemData$itemAttributes );
  88.     }
  89.  
  90.     /**
  91.      * Restores the data with the given $itemId from the storage configured in $conf.
  92.      *
  93.      * This method restores the item data identified by $itemId and optionally
  94.      * $itemAttributes from the {@link ezcCacheStackableStorage} given in
  95.      * $conf using {@link ezcCacheStackableStorage::restore()}. The result of
  96.      * this action is returned by the method. This means, the desired item data
  97.      * is returned on success, false is returned if the data is not available.
  98.      *
  99.      * A successful restore is recorded in $metaData as a "recent usage", with
  100.      * incrementing the usage counter of $itemId by 1. A restore failure
  101.      * results in a removal of $itemId.
  102.      *
  103.      * @param ezcCacheStackStorageConfiguration $conf 
  104.      * @param ezcCacheStackMetaData $metaData 
  105.      * @param string $itemId 
  106.      * @param array(string=>string) $itemAttributes 
  107.      * @param bool $search 
  108.      *
  109.      * @see ezcCacheStackReplacementStrategy::restore()
  110.      *
  111.      * @return mixed Restored data or false.
  112.      *
  113.      * @throws ezcCacheInvalidMetaDataException
  114.      *          if the given $metaData is not an instance of {@link }
  115.      *          ezcCacheStackLfuMetaData}.
  116.      */
  117.     public static function restore(
  118.         ezcCacheStackStorageConfiguration $conf,
  119.         ezcCacheStackMetaData $metaData,
  120.         $itemId,
  121.         $itemAttributes array(),
  122.         $search false
  123.     )
  124.     {
  125.         self::checkMetaData$metaData );
  126.         return parent::restore$conf$metaData$itemId$itemAttributes$search );
  127.     }
  128.  
  129.     /**
  130.      * Deletes the data with the given $itemId from the given $storage.
  131.      *
  132.      * Deletes the desired item with $itemId and optionally $itemAttributes
  133.      * from the {@link ezcCacheStackableStorage} configured in $conf using. The
  134.      * item IDs returned by this call are updated in $metaData, that they are
  135.      * no longer stored in the $storage.
  136.      *
  137.      * @param ezcCacheStackStorageConfiguration $conf 
  138.      * @param ezcCacheStackMetaData $metaData 
  139.      * @param string $itemId 
  140.      * @param array(string=>string) $itemAttributes 
  141.      * @param bool $search 
  142.      *
  143.      * @see ezcCacheStackReplacementStrategy::delete()
  144.      *
  145.      * @return array(string) Deleted item IDs.
  146.      *
  147.      * @throws ezcCacheInvalidMetaDataException
  148.      *          if the given $metaData is not an instance of {@link }
  149.      *          ezcCacheStackLfuMetaData}.
  150.      */
  151.     public static function delete(
  152.         ezcCacheStackStorageConfiguration $conf,
  153.         ezcCacheStackMetaData $metaData,
  154.         $itemId,
  155.         $itemAttributes array(),
  156.         $search false
  157.     )
  158.     {
  159.         self::checkMetaData$metaData );
  160.         return parent::delete$conf$metaData$itemId$itemAttributes$search );
  161.     }
  162.  
  163.     /**
  164.      * Returns a fresh meta data instance.
  165.      *
  166.      * Returns a freshly created instance of {@link ezcCacheStackLfuMetaData}.
  167.      * 
  168.      * @return ezcCacheStackLfuMetaData 
  169.      */
  170.     public static function createMetaData()
  171.     {
  172.         return new ezcCacheStackLfuMetaData();
  173.     }
  174.  
  175.     /**
  176.      * Checks if the given meta data is processable.
  177.      *
  178.      * Throws an exception if the given meta data is not processable.
  179.      * 
  180.      * @param ezcCacheStackMetaData $metaData 
  181.      *
  182.      * @throws ezcCacheInvalidMetaDataException
  183.      *          if the given $metaData is not an instance of {@link }
  184.      *          ezcCacheStackLfuMetaData}.
  185.      *
  186.      * @access private
  187.      */
  188.     protected static function checkMetaDataezcCacheStackMetaData $metaData )
  189.     {
  190.         if !$metaData instanceof ezcCacheStackLfuMetaData ) )
  191.         {
  192.             throw new ezcCacheInvalidMetaDataException(
  193.                 $metaData,
  194.                 'ezcCacheStackLfuMetaData'
  195.             );
  196.         }
  197.     }
  198. }
  199.  
  200. ?>
Documentation generated by phpDocumentor 1.4.3