MediaWiki  1.29.2
ProcessCacheLRU.php
Go to the documentation of this file.
1 <?php
23 use Wikimedia\Assert\Assert;
24 
31  protected $cache = []; // (key => prop => value)
32 
34  protected $cacheTimes = []; // (key => prop => UNIX timestamp)
35 
36  protected $maxCacheKeys; // integer; max entries
37 
42  public function __construct( $maxKeys ) {
43  $this->resize( $maxKeys );
44  }
45 
56  public function set( $key, $prop, $value ) {
57  if ( isset( $this->cache[$key] ) ) {
58  $this->ping( $key );
59  } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
60  reset( $this->cache );
61  $evictKey = key( $this->cache );
62  unset( $this->cache[$evictKey] );
63  unset( $this->cacheTimes[$evictKey] );
64  }
65  $this->cache[$key][$prop] = $value;
66  $this->cacheTimes[$key][$prop] = microtime( true );
67  }
68 
77  public function has( $key, $prop, $maxAge = 0.0 ) {
78  if ( isset( $this->cache[$key][$prop] ) ) {
79  return ( $maxAge <= 0 ||
80  ( microtime( true ) - $this->cacheTimes[$key][$prop] ) <= $maxAge
81  );
82  }
83 
84  return false;
85  }
86 
96  public function get( $key, $prop ) {
97  if ( !isset( $this->cache[$key][$prop] ) ) {
98  return null;
99  }
100  $this->ping( $key );
101  return $this->cache[$key][$prop];
102  }
103 
110  public function clear( $keys = null ) {
111  if ( $keys === null ) {
112  $this->cache = [];
113  $this->cacheTimes = [];
114  } else {
115  foreach ( (array)$keys as $key ) {
116  unset( $this->cache[$key] );
117  unset( $this->cacheTimes[$key] );
118  }
119  }
120  }
121 
129  public function resize( $maxKeys ) {
130  Assert::parameterType( 'integer', $maxKeys, '$maxKeys' );
131  Assert::parameter( $maxKeys > 0, '$maxKeys', 'must be above zero' );
132 
133  $this->maxCacheKeys = $maxKeys;
134  while ( count( $this->cache ) > $this->maxCacheKeys ) {
135  reset( $this->cache );
136  $evictKey = key( $this->cache );
137  unset( $this->cache[$evictKey] );
138  unset( $this->cacheTimes[$evictKey] );
139  }
140  }
141 
147  protected function ping( $key ) {
148  $item = $this->cache[$key];
149  unset( $this->cache[$key] );
150  $this->cache[$key] = $item;
151  }
152 
157  public function getSize() {
158  return $this->maxCacheKeys;
159  }
160 }
ProcessCacheLRU\resize
resize( $maxKeys)
Resize the maximum number of cache entries, removing older entries as needed.
Definition: ProcessCacheLRU.php:129
ProcessCacheLRU\getSize
getSize()
Get cache size.
Definition: ProcessCacheLRU.php:157
captcha-old.count
count
Definition: captcha-old.py:225
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:77
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\__construct
__construct( $maxKeys)
Definition: ProcessCacheLRU.php:42
ProcessCacheLRU\$cacheTimes
Array $cacheTimes
Definition: ProcessCacheLRU.php:34
$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:110
ProcessCacheLRU\ping
ping( $key)
Push an entry to the top of the cache.
Definition: ProcessCacheLRU.php:147
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:65
ProcessCacheLRU\$cache
Array $cache
Definition: ProcessCacheLRU.php:31
ProcessCacheLRU
Handles per process caching of items.
Definition: ProcessCacheLRU.php:29
ProcessCacheLRU\$maxCacheKeys
$maxCacheKeys
Definition: ProcessCacheLRU.php:36
array
the array() calling protocol came about after MediaWiki 1.4rc1.