MediaWiki  1.31.0
ProcessCacheLRU.php
Go to the documentation of this file.
1 <?php
23 use Wikimedia\Assert\Assert;
24 
34  protected $cache = []; // (key => prop => value)
35 
37  protected $cacheTimes = []; // (key => prop => UNIX timestamp)
38 
39  protected $maxCacheKeys; // integer; max entries
40 
45  public function __construct( $maxKeys ) {
46  $this->resize( $maxKeys );
47  }
48 
59  public function set( $key, $prop, $value ) {
60  if ( isset( $this->cache[$key] ) ) {
61  $this->ping( $key );
62  } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
63  reset( $this->cache );
64  $evictKey = key( $this->cache );
65  unset( $this->cache[$evictKey] );
66  unset( $this->cacheTimes[$evictKey] );
67  }
68  $this->cache[$key][$prop] = $value;
69  $this->cacheTimes[$key][$prop] = microtime( true );
70  }
71 
80  public function has( $key, $prop, $maxAge = 0.0 ) {
81  if ( isset( $this->cache[$key][$prop] ) ) {
82  return ( $maxAge <= 0 ||
83  ( microtime( true ) - $this->cacheTimes[$key][$prop] ) <= $maxAge
84  );
85  }
86 
87  return false;
88  }
89 
99  public function get( $key, $prop ) {
100  if ( !isset( $this->cache[$key][$prop] ) ) {
101  return null;
102  }
103  $this->ping( $key );
104  return $this->cache[$key][$prop];
105  }
106 
113  public function clear( $keys = null ) {
114  if ( $keys === null ) {
115  $this->cache = [];
116  $this->cacheTimes = [];
117  } else {
118  foreach ( (array)$keys as $key ) {
119  unset( $this->cache[$key] );
120  unset( $this->cacheTimes[$key] );
121  }
122  }
123  }
124 
132  public function resize( $maxKeys ) {
133  Assert::parameterType( 'integer', $maxKeys, '$maxKeys' );
134  Assert::parameter( $maxKeys > 0, '$maxKeys', 'must be above zero' );
135 
136  $this->maxCacheKeys = $maxKeys;
137  while ( count( $this->cache ) > $this->maxCacheKeys ) {
138  reset( $this->cache );
139  $evictKey = key( $this->cache );
140  unset( $this->cache[$evictKey] );
141  unset( $this->cacheTimes[$evictKey] );
142  }
143  }
144 
150  protected function ping( $key ) {
151  $item = $this->cache[$key];
152  unset( $this->cache[$key] );
153  $this->cache[$key] = $item;
154  }
155 
160  public function getSize() {
161  return $this->maxCacheKeys;
162  }
163 }
ProcessCacheLRU\resize
resize( $maxKeys)
Resize the maximum number of cache entries, removing older entries as needed.
Definition: ProcessCacheLRU.php:132
ProcessCacheLRU\getSize
getSize()
Get cache size.
Definition: ProcessCacheLRU.php:160
captcha-old.count
count
Definition: captcha-old.py:249
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ProcessCacheLRU\has
has( $key, $prop, $maxAge=0.0)
Check if a property field exists for a cache entry.
Definition: ProcessCacheLRU.php:80
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
key
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition: design.txt:25
ProcessCacheLRU\$cache
array $cache
Definition: ProcessCacheLRU.php:34
ProcessCacheLRU\__construct
__construct( $maxKeys)
Definition: ProcessCacheLRU.php:45
ProcessCacheLRU\$cacheTimes
array $cacheTimes
Definition: ProcessCacheLRU.php:37
$value
$value
Definition: styleTest.css.php:45
ProcessCacheLRU\clear
clear( $keys=null)
Clear one or several cache entries, or all cache entries.
Definition: ProcessCacheLRU.php:113
ProcessCacheLRU\ping
ping( $key)
Push an entry to the top of the cache.
Definition: ProcessCacheLRU.php:150
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$keys
$keys
Definition: testCompression.php:67
ProcessCacheLRU
Class for process caching individual properties of expiring items.
Definition: ProcessCacheLRU.php:32
ProcessCacheLRU\$maxCacheKeys
$maxCacheKeys
Definition: ProcessCacheLRU.php:39
array
the array() calling protocol came about after MediaWiki 1.4rc1.