all repos — wp-ffpc @ 457ad184c4316323fb9305f4084f066625367181

backends/memcache.php (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
<?php

if (!class_exists('WP_FFPC_Backend_memcache')):

class WP_FFPC_Backend_memcache extends WP_FFPC_Backend {
	/**
	 * init memcache backend
	 */
	protected  function _init () {
		/* Memcached class does not exist, Memcache extension is not available */
		if (!class_exists('Memcache')) {
			$this->log (  __translate__('PHP Memcache extension missing', $this->plugin_constant ), LOG_WARNING );
			return false;
		}

		/* check for existing server list, otherwise we cannot add backends */
		if ( empty ( $this->options['servers'] ) && ! $this->alive ) {
			$this->log (  __translate__("servers list is empty, init failed", $this->plugin_constant ), LOG_WARNING );
			return false;
		}

		/* check is there's no backend connection yet */
		if ( $this->connection === NULL )
			$this->connection = new Memcache();

		/* check if initialization was success or not */
		if ( $this->connection === NULL ) {
			$this->log (  __translate__( 'error initializing Memcache PHP extension, exiting', $this->plugin_constant ) );
			return false;
		}

		/* adding servers */
		foreach ( $this->options['servers'] as $server_id => $server ) {
				/* in case of unix socket */
			if ( $server['port'] === 0 )
				$this->status[$server_id] = $this->connection->connect ( 'unix:/' . $server['host'] );
			else
				$this->status[$server_id] = $this->connection->connect ( $server['host'] , $server['port'] );

			$this->log ( sprintf( __translate__( '%s added', $this->plugin_constant ),  $server_id ) );
		}

		/* backend is now alive */
		$this->alive = true;
		$this->_status();
	}

	/**
	 * check current backend alive status for Memcached
	 *
	 */
	protected  function _status () {
		/* server status will be calculated by getting server stats */
		$this->log (  __translate__("checking server statuses", $this->plugin_constant ));
		/* get servers statistic from connection */
		foreach ( $this->options['servers'] as $server_id => $server ) {
			if ( $server['port'] === 0 )
				$this->status[$server_id] = $this->connection->getServerStatus( $server['host'], 11211 );
			else
				$this->status[$server_id] = $this->connection->getServerStatus( $server['host'], $server['port'] );
			if ( $this->status[$server_id] == 0 )
				$this->log ( sprintf( __translate__( '%s server is down', $this->plugin_constant ),  $server_id ) );
			else
				$this->log ( sprintf( __translate__( '%s server is up & running', $this->plugin_constant ),  $server_id ) );
		}
	}

	/**
	 * get function for Memcached backend
	 *
	 * @param string $key Key to get values for
	 *
	*/
	protected  function _get ( &$key ) {
		return $this->connection->get($key);
	}

	/**
	 * Set function for Memcached backend
	 *
	 * @param string $key Key to set with
	 * @param mixed $data Data to set
	 *
	 */
	protected  function _set ( &$key, &$data, &$expire ) {
		$result = $this->connection->set ( $key, $data , 0 , $expire );
		return $result;
	}

	/**
	 *
	 * Flush memcached entries
	 */
	protected  function _flush ( ) {
		return $this->connection->flush();
	}


	/**
	 * Removes entry from Memcached or flushes Memcached storage
	 *
	 * @param mixed $keys String / array of string of keys to delete entries with
	*/
	protected  function _clear ( &$keys ) {
		/* make an array if only one string is present, easier processing */
		if ( !is_array ( $keys ) )
			$keys = array ( $keys => true );

		foreach ( $keys as $key => $dummy ) {
			$kresult = $this->connection->delete( $key );

			if ( $kresult === false ) {
				$this->log ( sprintf( __translate__( 'unable to delete entry: %s', $this->plugin_constant ),  $key ) );
			}
			else {
				$this->log ( sprintf( __translate__( 'entry deleted: %s', $this->plugin_constant ),  $key ) );
			}
		}
	}
}

endif;