5 * Contains \Drupal\memcache\DrupalMemcacheFactory.
8 namespace Drupal\memcache;
13 * Factory class for creation of Memcache objects.
15 class DrupalMemcacheFactory {
18 * The settings object.
20 * @var \Drupal\memcache\DrupalMemcacheConfig
32 protected $memcachePersistent;
35 * @var \Drupal\memcache\DrupalMemcacheInterface[]
37 protected $memcacheCache = array();
42 protected $memcacheServers = array();
47 protected $memcacheBins = array();
52 protected $failedConnectionCache = array();
55 * Constructs a DrupalMemcacheFactory object.
57 * @param \Drupal\memcache\DrupalMemcacheConfig $settings
59 public function __construct(DrupalMemcacheConfig $settings) {
60 $this->settings = $settings;
66 * Returns a Memcache object based on settings and the bin requested.
69 * The bin which is to be used.
72 * Rebuild the bin/server/cache mapping.
74 * @return \Drupal\memcache\DrupalMemcacheInterface
77 public function get($bin = NULL, $flush = FALSE) {
82 if (empty($this->memcacheCache) || empty($this->memcacheCache[$bin])) {
83 // If there is no cluster for this bin in $memcache_bins, cluster is
85 $cluster = empty($this->memcacheBins[$bin]) ? 'default' : $this->memcacheBins[$bin];
87 // If this bin isn't in our $memcacheBins configuration array, and the
88 // 'default' cluster is already initialized, map the bin to 'default'
89 // because we always map the 'default' bin to the 'default' cluster.
90 if (empty($this->memcacheBins[$bin]) && !empty($this->memcacheCache['default'])) {
91 $this->memcacheCache[$bin] = &$this->memcacheCache['default'];
94 // Create a new Memcache object. Each cluster gets its own Memcache
96 // @todo Can't add a custom memcache class here yet.
97 if ($this->extension == 'Memcached') {
98 $memcache = new DrupalMemcached($this->settings);
100 elseif ($this->extension == 'Memcache') {
101 $memcache = new DrupalMemcache($this->settings);
104 // A variable to track whether we've connected to the first server.
107 // Link all the servers to this cluster.
108 foreach ($this->memcacheServers as $s => $c) {
109 if ($c == $cluster && !isset($this->failedConnectionCache[$s])) {
110 if ($memcache->addServer($s, $this->memcachePersistent) && !$init) {
115 $this->failedConnectionCache[$s] = FALSE;
121 // Map the current bin with the new Memcache object.
122 $this->memcacheCache[$bin] = $memcache;
124 // Now that all the servers have been mapped to this cluster, look for
125 // other bins that belong to the cluster and map them too.
126 foreach ($this->memcacheBins as $b => $c) {
127 if (($c == $cluster) && ($b != $bin)) {
128 // Map this bin and cluster by reference.
129 $this->memcacheCache[$b] = &$this->memcacheCache[$bin];
134 throw new MemcacheException('Memcache instance could not be initialized. Check memcache is running and reachable');
139 return empty($this->memcacheCache[$bin]) ? FALSE : $this->memcacheCache[$bin];
143 * Initializes memcache settings.
145 protected function initialize() {
146 // If an extension is specified in settings.php, use that when available.
147 $preferred = $this->settings->get('extension', NULL);
148 if (isset($preferred) && class_exists($preferred)) {
149 $this->extension = $preferred;
151 // If no extension is set, default to Memcache. The Memcached extension has
152 // some features that the older extension lacks but also an unfixed bug that
153 // affects cache clears.
154 // @see http://pecl.php.net/bugs/bug.php?id=16829
155 elseif (class_exists('Memcache')) {
156 $this->extension = 'Memcache';
158 elseif (class_exists('Memcached')) {
159 $this->extension = 'Memcached';
162 throw new MemcacheException('No Memcache extension found');
165 // Values from settings.php
166 $this->memcacheServers = $this->settings->get('servers', ['127.0.0.1:11211' => 'default']);
167 $this->memcacheBins = $this->settings->get('bins', ['default' => 'default']);
169 // Indicate whether to connect to memcache using a persistent connection.
170 // Note: this only affects the Memcache PECL extension, and does not affect
171 // the Memcached PECL extension. For a detailed explanation see:
172 // http://drupal.org/node/822316#comment-4427676
173 $this->memcachePersistent = $this->settings->get('persistent', FALSE);
177 * Flushes the memcache bin/server/cache mappings and closes connections.
179 protected function flush() {
180 foreach ($this->memcacheCache as $cluster) {
184 $this->memcacheCache = array();