├── tests ├── tests │ ├── core.php │ ├── decrement.php │ ├── increment.php │ ├── fetch.php │ ├── delete.php │ ├── replace.php │ ├── cas.php │ ├── prepend.php │ ├── append.php │ ├── other.php │ ├── add.php │ ├── set.php │ ├── get-multi.php │ ├── get.php │ └── get-delayed.php ├── phpunit.xml ├── bootstrap.php ├── base.php └── bin │ └── install-wp-tests.sh ├── .travis.yml └── readme.md /tests/tests/core.php: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | ./tests/ 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | > ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini 19 | 20 | script: cd tests && phpunit 21 | 22 | services: 23 | - memcached 24 | -------------------------------------------------------------------------------- /tests/base.php: -------------------------------------------------------------------------------- 1 | array( '127.0.0.1', 11211 ), 18 | ); 19 | } 20 | 21 | // Instantiate the core cache tests and use that setup routine 22 | $this->test_cache = new Tests_Cache(); 23 | $this->test_cache->setUp(); 24 | 25 | $this->object_cache = $this->test_cache->cache; 26 | $this->servers = $this->object_cache->servers; 27 | } 28 | 29 | public function tearDown() { 30 | $this->test_cache->tearDown(); 31 | } 32 | } -------------------------------------------------------------------------------- /tests/tests/decrement.php: -------------------------------------------------------------------------------- 1 | assertTrue( $this->object_cache->set( $key, $value ) ); 11 | 12 | // Verify value 13 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 14 | 15 | // Verify that value was properly decremented 16 | $this->assertSame( 98, $this->object_cache->decrement( $key ) ); 17 | } 18 | 19 | public function test_decrement_reduces_value_by_x() { 20 | $key = microtime(); 21 | 22 | $value = 99; 23 | $x = 5; 24 | 25 | $reduced_value = $value - $x; 26 | 27 | // Verify set 28 | $this->assertTrue( $this->object_cache->set( $key, $value ) ); 29 | 30 | // Verify value 31 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 32 | 33 | // Verify that value was properly decremented 34 | $this->assertSame( $reduced_value, $this->object_cache->decrement( $key, $x ) ); 35 | } 36 | 37 | public function test_decrement_reduces_value_by_1_for_no_mc_group() { 38 | $key = microtime(); 39 | 40 | $value = 99; 41 | $group = 'counts'; 42 | 43 | // Verify set 44 | $this->assertTrue( $this->object_cache->set( $key, $value, $group ) ); 45 | 46 | // Verify value 47 | $this->assertSame( $value, $this->object_cache->get( $key, $group ) ); 48 | 49 | // Verify that value was properly decremented 50 | $this->assertSame( 98, $this->object_cache->decrement( $key, 1, $group ) ); 51 | } 52 | 53 | public function test_decrement_reduces_value_by_x_for_no_mc_group() { 54 | $key = microtime(); 55 | 56 | $value = 99; 57 | $x = 5; 58 | 59 | $group = 'counts'; 60 | 61 | $reduced_value = $value - $x; 62 | 63 | // Verify set 64 | $this->assertTrue( $this->object_cache->set( $key, $value, $group ) ); 65 | 66 | // Verify value 67 | $this->assertSame( $value, $this->object_cache->get( $key, $group ) ); 68 | 69 | // Verify that value was properly decremented 70 | $this->assertSame( $reduced_value, $this->object_cache->decrement( $key, $x, $group ) ); 71 | } 72 | } -------------------------------------------------------------------------------- /tests/tests/increment.php: -------------------------------------------------------------------------------- 1 | assertTrue( $this->object_cache->set( $key, $value ) ); 11 | 12 | // Verify value 13 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 14 | 15 | // Verify that value was properly incremented 16 | $this->assertSame( 100, $this->object_cache->increment( $key ) ); 17 | } 18 | 19 | public function test_increment_reduces_value_by_x() { 20 | $key = microtime(); 21 | 22 | $value = 99; 23 | $x = 5; 24 | 25 | $reduced_value = $value + $x; 26 | 27 | // Verify set 28 | $this->assertTrue( $this->object_cache->set( $key, $value ) ); 29 | 30 | // Verify value 31 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 32 | 33 | // Verify that value was properly incremented 34 | $this->assertSame( $reduced_value, $this->object_cache->increment( $key, $x ) ); 35 | } 36 | 37 | public function test_increment_reduces_value_by_1_for_no_mc_group() { 38 | $key = microtime(); 39 | 40 | $value = 99; 41 | $group = 'counts'; 42 | 43 | // Verify set 44 | $this->assertTrue( $this->object_cache->set( $key, $value, $group ) ); 45 | 46 | // Verify value 47 | $this->assertSame( $value, $this->object_cache->get( $key, $group ) ); 48 | 49 | // Verify that value was properly incremented 50 | $this->assertSame( 100, $this->object_cache->increment( $key, 1, $group ) ); 51 | } 52 | 53 | public function test_increment_reduces_value_by_x_for_no_mc_group() { 54 | $key = microtime(); 55 | 56 | $value = 99; 57 | $x = 5; 58 | 59 | $group = 'counts'; 60 | 61 | $reduced_value = $value + $x; 62 | 63 | // Verify set 64 | $this->assertTrue( $this->object_cache->set( $key, $value, $group ) ); 65 | 66 | // Verify value 67 | $this->assertSame( $value, $this->object_cache->get( $key, $group ) ); 68 | 69 | // Verify that value was properly incremented 70 | $this->assertSame( $reduced_value, $this->object_cache->increment( $key, $x, $group ) ); 71 | } 72 | } -------------------------------------------------------------------------------- /tests/tests/fetch.php: -------------------------------------------------------------------------------- 1 | 66, 7 | microtime() . '-jagr' => 'sixty-eight' 8 | ); 9 | 10 | $rekeyed = array(); 11 | foreach ( $keys as $key => $value ) 12 | $rekeyed[$this->object_cache->buildKey( $key )] = $value; 13 | 14 | // Set each value 15 | foreach ( $keys as $key => $value ) { 16 | $this->assertTrue( $this->object_cache->set( $key, $value ) ); 17 | } 18 | 19 | // Verify each value 20 | foreach ( $keys as $key => $value ) { 21 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 22 | } 23 | 24 | // getDelayed to retrieve the objects 25 | $this->assertTrue( $this->object_cache->getDelayed( array_keys( $keys ) ) ); 26 | 27 | // Loop through the initial values and see if an appropriate value was returned. Note that they likely won't come back in the same order. 28 | foreach ( $keys as $key => $value ) { 29 | $next = $this->object_cache->fetch(); 30 | $this->assertTrue( isset( $rekeyed[$next['key']] ) ); 31 | $this->assertSame( $rekeyed[$next['key']], $next['value'] ); 32 | } 33 | 34 | // Doing another fetch call should result in false 35 | $this->assertFalse( $this->object_cache->fetch() ); 36 | 37 | // Finally, that last operation should result in the result code of Memcached::RES_NOTFOUND 38 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 39 | } 40 | 41 | public function test_fetch_all_gets_all_results() { 42 | $keys = array( 43 | microtime() . '-lemieux' => 66, 44 | microtime() . '-jagr' => 'sixty-eight' 45 | ); 46 | 47 | // Set each value 48 | foreach ( $keys as $key => $value ) { 49 | $this->assertTrue( $this->object_cache->set( $key, $value ) ); 50 | } 51 | 52 | // Verify each value 53 | foreach ( $keys as $key => $value ) { 54 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 55 | } 56 | 57 | // getDelayed to retrieve the objects 58 | $this->assertTrue( $this->object_cache->getDelayed( array_keys( $keys ) ) ); 59 | 60 | // Build the final expect result array 61 | $result_array = array(); 62 | foreach ( $keys as $key => $value ) { 63 | $result_array[] = array( 'key' => $this->object_cache->buildKey( $key, 'default' ), 'value' => $value ); 64 | } 65 | 66 | // Make sure fetchAll returns expected array 67 | $this->assertTrue( $result_array === $this->object_cache->fetchAll() ); 68 | 69 | // Doing another fetch call should result in false 70 | $this->assertFalse( $this->object_cache->fetchAll() ); 71 | 72 | // Finally, that last operation should result in the result code of Memcached::RES_NOTFOUND 73 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 74 | } 75 | } -------------------------------------------------------------------------------- /tests/bin/install-wp-tests.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [ $# -lt 3 ]; then 4 | echo "usage: $0 [db-host] [wp-version]" 5 | exit 1 6 | fi 7 | 8 | DB_NAME=$1 9 | DB_USER=$2 10 | DB_PASS=$3 11 | DB_HOST=${4-localhost} 12 | WP_VERSION=${5-master} 13 | 14 | WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib} 15 | WP_CORE_DIR=/tmp/wordpress/ 16 | 17 | set -ex 18 | 19 | install_wp() { 20 | mkdir -p $WP_CORE_DIR 21 | 22 | if [ $WP_VERSION == 'latest' ]; then 23 | local ARCHIVE_NAME='latest' 24 | else 25 | local ARCHIVE_NAME="wordpress-$WP_VERSION" 26 | fi 27 | 28 | wget -nv -O /tmp/wordpress.tar.gz http://wordpress.org/${ARCHIVE_NAME}.tar.gz 29 | tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR 30 | 31 | wget -nv -O $WP_CORE_DIR/wp-content/db.php https://raw.github.com/markoheijnen/wp-mysqli/master/db.php 32 | } 33 | 34 | install_test_suite() { 35 | # portable in-place argument for both GNU sed and Mac OSX sed 36 | if [[ $(uname -s) == 'Darwin' ]]; then 37 | local ioption='-i .bak' 38 | else 39 | local ioption='-i' 40 | fi 41 | 42 | # set up testing suite 43 | mkdir -p $WP_TESTS_DIR 44 | cd $WP_TESTS_DIR 45 | svn co --quiet http://develop.svn.wordpress.org/trunk/tests/phpunit/includes/ 46 | 47 | wget -nv -O wp-tests-config.php http://develop.svn.wordpress.org/trunk/wp-tests-config-sample.php 48 | sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" wp-tests-config.php 49 | sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" wp-tests-config.php 50 | sed $ioption "s/yourusernamehere/$DB_USER/" wp-tests-config.php 51 | sed $ioption "s/yourpasswordhere/$DB_PASS/" wp-tests-config.php 52 | sed $ioption "s|localhost|${DB_HOST}|" wp-tests-config.php 53 | 54 | # Grab the cache tests from core to test against them 55 | mkdir -p $WP_TESTS_DIR/tests 56 | wget -nv -O $WP_TESTS_DIR/tests/cache.php http://develop.svn.wordpress.org/trunk/tests/phpunit/tests/cache.php 57 | 58 | # Setup memcached servers 59 | echo 'global $memcached_servers;' >> wp-tests-config.php 60 | echo '$memcached_servers = array(' >> wp-tests-config.php 61 | echo ' array(' >> wp-tests-config.php 62 | echo ' "127.0.0.1",' >> wp-tests-config.php 63 | echo ' 11211' >> wp-tests-config.php 64 | echo ' )' >> wp-tests-config.php 65 | echo ');' >> wp-tests-config.php 66 | } 67 | 68 | install_db() { 69 | # parse DB_HOST for port or socket references 70 | local PARTS=(${DB_HOST//\:/ }) 71 | local DB_HOSTNAME=${PARTS[0]}; 72 | local DB_SOCK_OR_PORT=${PARTS[1]}; 73 | local EXTRA="" 74 | 75 | if ! [ -z $DB_HOSTNAME ] ; then 76 | if [[ "$DB_SOCK_OR_PORT" =~ ^[0-9]+$ ]] ; then 77 | EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp" 78 | elif ! [ -z $DB_SOCK_OR_PORT ] ; then 79 | EXTRA=" --socket=$DB_SOCK_OR_PORT" 80 | elif ! [ -z $DB_HOSTNAME ] ; then 81 | EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" 82 | fi 83 | fi 84 | 85 | # create database 86 | mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA 87 | } 88 | 89 | install_wp 90 | install_test_suite 91 | install_db 92 | -------------------------------------------------------------------------------- /tests/tests/delete.php: -------------------------------------------------------------------------------- 1 | assertTrue( $this->object_cache->set( $key, $value ) ); 12 | 13 | // Verify value 14 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 15 | 16 | // Verify delete 17 | $this->assertTrue( $this->object_cache->delete( $key ) ); 18 | 19 | // Verify that key is not gettable after delete 20 | $this->assertFalse( $this->object_cache->get( $key ) ); 21 | 22 | // Verify that I can add a new value with this key 23 | $this->assertTrue( $this->object_cache->add( $key, $value2 ) ); 24 | 25 | // Verify the new value 26 | $this->assertSame( $value2, $this->object_cache->get( $key ) ); 27 | } 28 | 29 | public function test_delete_key_from_no_mc_group() { 30 | $key = microtime(); 31 | 32 | $value = 'sasquatch'; 33 | $value2 = 'yeti'; 34 | 35 | $group = 'comment'; 36 | 37 | // Verify set 38 | $this->assertTrue( $this->object_cache->set( $key, $value, $group ) ); 39 | 40 | // Verify value 41 | $this->assertSame( $value, $this->object_cache->get( $key, $group ) ); 42 | 43 | // Verify value is in the internal cache 44 | $this->assertSame( $value, $this->object_cache->cache[ $this->object_cache->buildKey( $key, $group ) ] ); 45 | 46 | // Verify delete 47 | $this->assertTrue( $this->object_cache->delete( $key, $group ) ); 48 | 49 | // Verify that key is not gettable after delete 50 | $this->assertFalse( $this->object_cache->get( $key, $group ) ); 51 | 52 | // Verify that I can add a new value with this key 53 | $this->assertTrue( $this->object_cache->add( $key, $value2, $group ) ); 54 | 55 | // Verify the new value 56 | $this->assertSame( $value2, $this->object_cache->get( $key, $group ) ); 57 | } 58 | 59 | public function test_delete_by_key() { 60 | $key = microtime(); 61 | 62 | $value = 'sasquatch'; 63 | $value2 = 'yeti'; 64 | 65 | $server_key = 'my-server1'; 66 | 67 | // Verify set 68 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value ) ); 69 | 70 | // Verify value 71 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 72 | 73 | // Verify delete 74 | $this->assertTrue( $this->object_cache->deleteByKey( $server_key, $key ) ); 75 | 76 | // Verify that key is not gettable after delete 77 | $this->assertFalse( $this->object_cache->getByKey( $server_key, $key ) ); 78 | 79 | // Verify that I can add a new value with this key 80 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value2 ) ); 81 | 82 | // Verify the new value 83 | $this->assertSame( $value2, $this->object_cache->getByKey( $server_key, $key ) ); 84 | } 85 | 86 | public function test_delete_by_key_from_no_mc_group() { 87 | $key = microtime(); 88 | 89 | $value = 'sasquatch'; 90 | $value2 = 'yeti'; 91 | 92 | $server_key = 'my-server1'; 93 | 94 | $group = 'comment'; 95 | 96 | // Verify set 97 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value, $group ) ); 98 | 99 | // Verify value 100 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, $group ) ); 101 | 102 | // Verify delete 103 | $this->assertTrue( $this->object_cache->deleteByKey( $server_key, $key, $group ) ); 104 | 105 | // Verify that key is not gettable after delete 106 | $this->assertFalse( $this->object_cache->getByKey( $server_key, $key, $group ) ); 107 | 108 | // Verify that I can add a new value with this key 109 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value2, $group ) ); 110 | 111 | // Verify the new value 112 | $this->assertSame( $value2, $this->object_cache->getByKey( $server_key, $key, $group ) ); 113 | } 114 | } -------------------------------------------------------------------------------- /tests/tests/replace.php: -------------------------------------------------------------------------------- 1 | assertTrue( $this->object_cache->add( $key, $value1 ) ); 11 | 12 | $this->assertSame( $value1, $this->object_cache->get( $key ) ); 13 | 14 | $this->assertTrue( $this->object_cache->replace( $key, $value2 ) ); 15 | 16 | $this->assertSame( $value2, $this->object_cache->get( $key ) ); 17 | } 18 | 19 | public function test_replace_value_when_key_is_not_set() { 20 | $key = microtime(); 21 | 22 | $value = 'parise'; 23 | 24 | $this->assertFalse( $this->object_cache->replace( $key, $value ) ); 25 | 26 | $this->assertFalse( $this->object_cache->get( $key ) ); 27 | } 28 | 29 | public function test_replace_by_key_value_with_another_value() { 30 | $key = microtime(); 31 | 32 | $value1 = 'parise'; 33 | $value2 = 'kovalchuk'; 34 | 35 | $server_key = 'my-server'; 36 | 37 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value1 ) ); 38 | 39 | $this->assertSame( $value1, $this->object_cache->getByKey( $server_key, $key ) ); 40 | 41 | $this->assertTrue( $this->object_cache->replaceByKey( $server_key, $key, $value2 ) ); 42 | 43 | $this->assertSame( $value2, $this->object_cache->getByKey( $server_key, $key ) ); 44 | } 45 | 46 | public function test_replace_by_key_value_when_key_is_not_set() { 47 | $key = microtime(); 48 | 49 | $value = 'parise'; 50 | 51 | $server_key = 'my-server'; 52 | 53 | $this->assertFalse( $this->object_cache->replaceByKey( $server_key, $key, $value ) ); 54 | 55 | $this->assertFalse( $this->object_cache->getByKey( $server_key, $key ) ); 56 | } 57 | 58 | public function test_replace_with_expiration_of_30_days() { 59 | $key = 'usa'; 60 | $value = 'merica'; 61 | $group = 'july'; 62 | $built_key = $this->object_cache->buildKey( $key, $group ); 63 | 64 | $value2 = 'belgium'; 65 | 66 | // 30 days 67 | $expiration = 60 * 60 * 24 * 30; 68 | 69 | $this->assertTrue( $this->object_cache->set( $key, $value, $group ) ); 70 | $this->assertTrue( $this->object_cache->replace( $key, $value2, $group, $expiration ) ); 71 | 72 | // Verify that the value is in cache by accessing memcached directly 73 | $this->assertEquals( $value2, $this->object_cache->m->get( $built_key ) ); 74 | 75 | // Remove the value from internal cache to force a lookup 76 | unset( $this->object_cache->cache[ $built_key ] ); 77 | 78 | // Verify that the value is no longer in the internal cache 79 | $this->assertFalse( $this->object_cache->get_from_runtime_cache( $key, $group ) ); 80 | 81 | // Do the lookup with the API to verify that we get the value 82 | $this->assertEquals( $value2, $this->object_cache->get( $key, $group ) ); 83 | } 84 | 85 | public function test_replace_with_expiration_longer_than_30_days() { 86 | $key = 'usa'; 87 | $value = 'merica'; 88 | $group = 'july'; 89 | $built_key = $this->object_cache->buildKey( $key, $group ); 90 | 91 | $value2 = 'belgium'; 92 | 93 | // 30 days and 1 second; if interpreted as timestamp, becomes "Sat, 31 Jan 1970 00:00:01 GMT" 94 | $expiration = 60 * 60 * 24 * 30 + 1; 95 | 96 | $this->assertTrue( $this->object_cache->set( $key, $value, $group ) ); 97 | $this->assertTrue( $this->object_cache->replace( $key, $value2, $group, $expiration ) ); 98 | 99 | // Verify that the value is in cache by accessing memcached directly 100 | $this->assertEquals( $value2, $this->object_cache->m->get( $built_key ) ); 101 | 102 | // Remove the value from internal cache to force a lookup 103 | unset( $this->object_cache->cache[ $built_key ] ); 104 | 105 | // Verify that the value is no longer in the internal cache 106 | $this->assertFalse( $this->object_cache->get_from_runtime_cache( $key, $group ) ); 107 | 108 | // Do the lookup with the API to verify that we get the value 109 | $this->assertEquals( $value2, $this->object_cache->get( $key, $group ) ); 110 | } 111 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/tollmanz/wordpress-memcached-backend.png?branch=master)](https://travis-ci.org/tollmanz/wordpress-memcached-backend) 2 | 3 | ## Overview 4 | 5 | This project is a WordPress object cache backend that implements all available methods in the [Memcached PECL extension](http://www.php.net/manual/en/class.memcached.php). For a detailed account of how this differs from a Memcache PECL backend (note the inclusion/exclusion of the "d"), read the [article I wrote on the topic](http://tollmanz.com/wordpress-memcached-object-cache/). 6 | 7 | ## Installation 8 | 9 | 1. Install the Memcached daemon. Memcached should be available via your favorite package manager in your Linux distribution of choice. 10 | 11 | For Ubuntu and Debian: 12 | 13 | ```bash 14 | apt-get install memcached 15 | ``` 16 | For CentOS: 17 | 18 | ```bash 19 | yum install memcached 20 | ``` 21 | 22 | Note that you will likely want to review the Memcached configuration [directives](http://serverfault.com/questions/347621/memcache-basic-configuration). To get the best results from Memcached, you will need to configure it for your system and use case. 23 | 24 | 1. Start the Memcached daemon: 25 | 26 | ```bash 27 | service memcached restart 28 | ``` 29 | 30 | 1. Verify that Memcached is installed and running. 31 | 32 | 1. From your server, `telnet` into the Memcached server 33 | 34 | ```bash 35 | telnet localhost 11211 36 | ``` 37 | 38 | You should see output like: 39 | 40 | ```bash 41 | Trying 127.0.0.1... 42 | Connected to localhost. 43 | Escape character is '^]'. 44 | ``` 45 | 46 | 1. Type `version` into the Telnet prompt. If Memcached is installed and running, you should see something like: 47 | 48 | ```bash 49 | VERSION 1.4.14 (Ubuntu) 50 | ``` 51 | 52 | 1. Exit Telnet by typing `ctrl` + `]`, hitting `enter`, then typing `quit` and pressing `enter`. 53 | 54 | 1. Install the Memcached PECL extension on your server. Note that there are two different PHP interfaces for Memcached; one is named PECL Memcache and the other, PECL Memcached. The "d" at the end of Memcached is extremely important in this case. You should be able to install PECL Memcached from your package manager for your Linux distro. 55 | 56 | For Ubuntu and Debian: 57 | 58 | ```bash 59 | apt-get install php5-memcached 60 | ``` 61 | 62 | For CentOS: 63 | 64 | ```bash 65 | yum install php-pecl-memcached 66 | ``` 67 | 68 | Note that if you have a more custom installation of PHP, you might need to take some extra steps to link the PECL Memcached extension to PHP. If you are setting this up using your package manager's version of PHP and PECL Memcached, this should not be necessary. For example, many `yum.conf` files will exclude packages that begin with `php`. You may be able to modify the configuration file as necessary to install this package. 69 | 70 | 1. Verify that the Memcached daemon is running and accessible via the PECL Memcached extension in PHP. The easiest way to test this is to run a simple PHP script that connects to Memcached and tries to set and retrieve a value. 71 | 72 | 1. Enter PHP's interactive shell: 73 | 74 | ```bash 75 | php -a 76 | ``` 77 | 78 | 1. Type the following in the interactive shell: 79 | 80 | ```bash 81 | php > $m = new Memcached(); 82 | php > $m->addServer( '127.0.0.1', 11211 ); 83 | php > $m->set( 'foo', 100 ); 84 | php > echo $m->get( 'foo' ) . "\n"; 85 | ``` 86 | 87 | 1. If that last command returns `100`, everything is working! If you get blank output, something is amiss. 88 | 89 | Note that if your PHP configuration does not support the interactive shell, you can use the code above to create a script to execute in the browser. The interactive shell is easier for the verification step as it does not require a functioning web server. 90 | 91 | 1. Now that the server dependencies are resolved, the rest of the configuration is in the WordPress application. Take the **object-cache.php** file and place it in your **wp-content** folder. For instance, if the root of your WordPress installation is at **/srv/www/wordpress**, the location of **object-cache.php** would be **/srv/www/wordpress/wp-content/object-cache.php**. Please note that **object-cache.php** is a [WordPress drop-in](http://hakre.wordpress.com/2010/05/01/must-use-and-drop-ins-plugins/). It is not a regular plugin or an MU plugin. Drop-ins are located directly in the **wp-content** folder. 92 | 93 | 1. Add the following to your **wp-config.php** file: 94 | 95 | ```php 96 | global $memcached_servers; 97 | $memcached_servers = array( 98 | array( 99 | '127.0.0.1', // Memcached server IP address 100 | 11211 // Memcached server port 101 | ) 102 | ); 103 | ``` 104 | 105 | If your Memcached server is located on a different server or port, adjust those values as needed. If you have multiple Memcached instances, add additional servers to the array: 106 | 107 | ```php 108 | global $memcached_servers; 109 | $memcached_servers = array( 110 | array( 111 | '1.2.3.4', 112 | 11211 113 | ), 114 | array( 115 | '1.2.3.5', 116 | 11211 117 | ) 118 | ); 119 | ``` 120 | 121 | 1. To test the WordPress object cache setup, add the following code as an MU plugin: 122 | 123 | ```php 124 | assertTrue( $this->object_cache->add( $key, $value ) ); 12 | 13 | // Get CAS token 14 | $this->assertSame( $value, $this->object_cache->get( $key, 'default', false, $found, '', false, null, $cas_token ) ); 15 | 16 | // Verify that we have a CAS token 17 | $this->assertTrue( is_float( $cas_token ) ); 18 | 19 | // Add value via cas 20 | $this->assertTrue( $this->object_cache->cas( $cas_token, $key, $new_value ) ); 21 | 22 | // Verify the value is correct 23 | $this->assertSame( $new_value, $this->object_cache->get( $key ) ); 24 | } 25 | 26 | public function test_cas_sets_internal_cache() { 27 | $key = microtime(); 28 | 29 | $value = 'ovechkin'; 30 | $new_value = 'crosby'; 31 | 32 | // Add value 33 | $this->assertTrue( $this->object_cache->add( $key, $value ) ); 34 | 35 | // Get CAS token 36 | $cas_token = ''; 37 | $this->assertSame( $value, $this->object_cache->get( $key, 'default', false, $found, '', false, null, $cas_token ) ); 38 | 39 | // Verify that we have a CAS token 40 | $this->assertTrue( is_float( $cas_token ) ); 41 | 42 | // Add value via cas 43 | $this->assertTrue( $this->object_cache->cas( $cas_token, $key, $new_value ) ); 44 | 45 | // Verify the value is correct 46 | $this->assertSame( $new_value, $this->object_cache->get( $key ) ); 47 | 48 | // Verify the internal cache is correctly set 49 | $this->assertSame( $new_value, $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 50 | } 51 | 52 | public function test_cas_by_key_sets_value_correctly() { 53 | $key = microtime(); 54 | 55 | $value = 'ovechkin'; 56 | $new_value = 'crosby'; 57 | 58 | $server_key = 'my-server1'; 59 | 60 | // Add value 61 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value ) ); 62 | 63 | // Get CAS token 64 | $cas_token = ''; 65 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, 'default', false, $found, null, $cas_token ) ); 66 | 67 | // Verify that we have a CAS token 68 | $this->assertTrue( is_float( $cas_token ) ); 69 | 70 | // Add value via cas 71 | $this->assertTrue( $this->object_cache->casByKey( $cas_token, $server_key, $key, $new_value ) ); 72 | 73 | // Verify the value is correct 74 | $this->assertSame( $new_value, $this->object_cache->getByKey( $server_key, $key ) ); 75 | } 76 | 77 | public function test_cas_by_key_sets_internal_cache() { 78 | $key = microtime(); 79 | 80 | $value = 'ovechkin'; 81 | $new_value = 'crosby'; 82 | 83 | $server_key = 'my-server1'; 84 | 85 | // Add value 86 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value ) ); 87 | 88 | // Get CAS token 89 | $cas_token = ''; 90 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, 'default', false, $found, null, $cas_token ) ); 91 | 92 | // Verify that we have a CAS token 93 | $this->assertTrue( is_float( $cas_token ) ); 94 | 95 | // Add value via cas 96 | $this->assertTrue( $this->object_cache->casByKey( $cas_token, $server_key, $key, $new_value ) ); 97 | 98 | // Verify the value is correct 99 | $this->assertSame( $new_value, $this->object_cache->getByKey( $server_key, $key ) ); 100 | 101 | // Verify the internal cache is correctly set 102 | $this->assertSame( $new_value, $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 103 | } 104 | 105 | public function test_cas_with_expiration_of_30_days() { 106 | $key = 'usa'; 107 | $value = 'merica'; 108 | $group = 'july'; 109 | $value2 = 'dutch'; 110 | $built_key = $this->object_cache->buildKey( $key, $group ); 111 | $found = false; 112 | $cas_token = ''; 113 | 114 | // 30 days 115 | $expiration = 60 * 60 * 24 * 30; 116 | 117 | $this->assertTrue( $this->object_cache->set( $key, $value, $group, $expiration ) ); 118 | $this->assertSame( $value, $this->object_cache->get( $key, $group, false, $found, '', false, null, $cas_token ) ); 119 | 120 | // Set a new value with the CAS method 121 | $this->assertTrue( $this->object_cache->cas( $cas_token, $key, $value2, $group, $expiration ) ); 122 | 123 | // Verify that the value is in cache by accessing memcached directly 124 | $this->assertEquals( $value2, $this->object_cache->m->get( $built_key ) ); 125 | 126 | // Remove the value from internal cache to force a lookup 127 | unset( $this->object_cache->cache[ $built_key ] ); 128 | 129 | // Verify that the value is no longer in the internal cache 130 | $this->assertFalse( $this->object_cache->get_from_runtime_cache( $key, $group ) ); 131 | 132 | // Do the lookup with the API to verify that we get the value 133 | $this->assertEquals( $value2, $this->object_cache->get( $key, $group ) ); 134 | } 135 | 136 | public function test_cas_with_expiration_longer_than_30_days() { 137 | $key = 'usa'; 138 | $value = 'merica'; 139 | $group = 'july'; 140 | $value2 = 'dutch'; 141 | $built_key = $this->object_cache->buildKey( $key, $group ); 142 | $found = false; 143 | $cas_token = ''; 144 | 145 | // 30 days and 1 second; if interpreted as timestamp, becomes "Sat, 31 Jan 1970 00:00:01 GMT" 146 | $expiration = 60 * 60 * 24 * 30 + 1; 147 | 148 | $this->assertTrue( $this->object_cache->set( $key, $value, $group, $expiration ) ); 149 | $this->assertSame( $value, $this->object_cache->get( $key, $group, false, $found, '', false, null, $cas_token ) ); 150 | 151 | // Set a new value with the CAS method 152 | $this->assertTrue( $this->object_cache->cas( $cas_token, $key, $value2, $group, $expiration ) ); 153 | 154 | // Verify that the value is in cache by accessing memcached directly 155 | $this->assertEquals( $value2, $this->object_cache->m->get( $built_key ) ); 156 | 157 | // Remove the value from internal cache to force a lookup 158 | unset( $this->object_cache->cache[ $built_key ] ); 159 | 160 | // Verify that the value is no longer in the internal cache 161 | $this->assertFalse( $this->object_cache->get_from_runtime_cache( $key, $group ) ); 162 | 163 | // Do the lookup with the API to verify that we get the value 164 | $this->assertEquals( $value2, $this->object_cache->get( $key, $group ) ); 165 | } 166 | } -------------------------------------------------------------------------------- /tests/tests/prepend.php: -------------------------------------------------------------------------------- 1 | assertTrue( $this->object_cache->add( $key, $value ) ); 15 | 16 | // Verify add 17 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 18 | 19 | // Append should fail because compression is on by default 20 | // Note, this should throw an exception, not return false 21 | $this->assertFalse( $this->object_cache->prepend( $prepended_value, $key ) ); 22 | } 23 | 24 | 25 | public function test_prepend_string_succeeds_with_compression_off() { 26 | $key = microtime(); 27 | 28 | $value = 'jordan'; 29 | $prepended_value = 'pippen'; 30 | $combined = $prepended_value . $value; 31 | 32 | // Turn compression off 33 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_COMPRESSION, false ) ); 34 | 35 | // Add value 36 | $this->assertTrue( $this->object_cache->add( $key, $value ) ); 37 | 38 | // Verify add 39 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 40 | 41 | // Append 42 | $this->assertTrue( $this->object_cache->prepend( $key, $prepended_value ) ); 43 | 44 | // Verify prepend 45 | $this->assertSame( $combined, $this->object_cache->get( $key ) ); 46 | 47 | // Turn compression back on 48 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_COMPRESSION, true ) ); 49 | } 50 | 51 | public function test_prepend_with_mixed_types() { 52 | $key = microtime(); 53 | 54 | $value = 23; 55 | $prepended_value = 45.42; 56 | $combined = $prepended_value . $value; 57 | settype( $combined, gettype( $value ) ); 58 | 59 | // Turn compression off 60 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_COMPRESSION, false ) ); 61 | 62 | // Add value 63 | $this->assertTrue( $this->object_cache->add( $key, $value ) ); 64 | 65 | // Verify add 66 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 67 | 68 | // Append 69 | $this->assertTrue( $this->object_cache->prepend( $key, $prepended_value ) ); 70 | 71 | // Verify prepend 72 | $this->assertSame( $combined, $this->object_cache->get( $key ) ); 73 | 74 | // Turn compression back on 75 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_COMPRESSION, true ) ); 76 | } 77 | 78 | public function test_prepend_array_should_fail() { 79 | $key = microtime(); 80 | 81 | $value = array( 'gretzky', 'messier' ); 82 | $prepended_value = array( 'kuri', 'fuhr' ); 83 | 84 | // Add value 85 | $this->assertTrue( $this->object_cache->add( $key, $value ) ); 86 | 87 | // Verify add 88 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 89 | 90 | // Append, which should fail due to data type 91 | $this->assertFalse( $this->object_cache->prepend( $key, $prepended_value ) ); 92 | } 93 | 94 | /** 95 | * @expectedException PHPUnit_Framework_Error 96 | */ 97 | public function test_prepend_by_key_fails_with_compression_on() { 98 | $key = microtime(); 99 | 100 | $value = 'jordan'; 101 | $prepended_value = 'pippen'; 102 | 103 | $server_key = 'bulls'; 104 | 105 | // Add value 106 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value ) ); 107 | 108 | // Verify add 109 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 110 | 111 | // Append should fail because compression is on by default 112 | // Note, this should throw an exception, not return false 113 | $this->assertFalse( $this->object_cache->prependByKey( $server_key, $prepended_value, $key ) ); 114 | } 115 | 116 | public function test_prepend_by_key_string_with_compression_off() { 117 | $key = microtime(); 118 | 119 | $value = 'jordan'; 120 | $prepended_value = 'pippen'; 121 | $combined = $prepended_value . $value; 122 | 123 | $server_key = 'bulls'; 124 | 125 | // Turn compression off 126 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_COMPRESSION, false ) ); 127 | 128 | // Add value 129 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value ) ); 130 | 131 | // Verify add 132 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 133 | 134 | // Append 135 | $this->assertTrue( $this->object_cache->prependByKey( $server_key, $key, $prepended_value ) ); 136 | 137 | // Verify prepend 138 | $this->assertSame( $combined, $this->object_cache->getByKey( $server_key, $key ) ); 139 | 140 | // Turn compression back on 141 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_COMPRESSION, true ) ); 142 | } 143 | 144 | public function test_prepend_by_key_with_mixed_types() { 145 | $key = microtime(); 146 | 147 | $value = 23; 148 | $prepended_value = 45.42; 149 | $combined = $prepended_value . $value; 150 | settype( $combined, gettype( $value ) ); 151 | 152 | $server_key = 'chicago'; 153 | 154 | // Turn compression off 155 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_COMPRESSION, false ) ); 156 | 157 | // Add value 158 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value ) ); 159 | 160 | // Verify add 161 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 162 | 163 | // Append 164 | $this->assertTrue( $this->object_cache->prependByKey( $server_key, $key, $prepended_value ) ); 165 | 166 | // Verify prepend 167 | $this->assertSame( $combined, $this->object_cache->getByKey( $server_key, $key ) ); 168 | 169 | // Turn compression back on 170 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_COMPRESSION, true ) ); 171 | } 172 | 173 | public function test_prepend_array_by_key_should_fail() { 174 | $key = microtime(); 175 | 176 | $value = array( 'gretzky', 'messier' ); 177 | $prepended_value = array( 'kuri', 'fuhr' ); 178 | 179 | $server_key = 'blackhawks'; 180 | 181 | // Add value 182 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value ) ); 183 | 184 | // Verify add 185 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 186 | 187 | // Append, which should fail due to data type 188 | $this->assertFalse( $this->object_cache->prependByKey( $server_key, $key, $prepended_value ) ); 189 | } 190 | } -------------------------------------------------------------------------------- /tests/tests/append.php: -------------------------------------------------------------------------------- 1 | assertTrue( $this->object_cache->add( $key, $value ) ); 15 | 16 | // Verify add 17 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 18 | 19 | // Append should fail because compression is on by default 20 | // Note, this should throw an exception, not return false 21 | $this->assertFalse( $this->object_cache->append( $appended_value, $key ) ); 22 | } 23 | 24 | 25 | public function test_append_string_succeeds_with_compression_off() { 26 | $key = microtime(); 27 | 28 | $value = 'jordan'; 29 | $appended_value = 'pippen'; 30 | $combined = $value . $appended_value; 31 | 32 | // Turn compression off 33 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_COMPRESSION, false ) ); 34 | 35 | // Add value 36 | $this->assertTrue( $this->object_cache->add( $key, $value ) ); 37 | 38 | // Verify add 39 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 40 | 41 | // Append 42 | $this->assertTrue( $this->object_cache->append( $key, $appended_value ) ); 43 | 44 | // Verify append 45 | $this->assertSame( $combined, $this->object_cache->get( $key ) ); 46 | 47 | // Turn compression back on 48 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_COMPRESSION, true ) ); 49 | } 50 | 51 | public function test_append_int_is_casted_to_string() { 52 | $key = microtime(); 53 | 54 | $value = 23; 55 | $appended_value = 45.42; 56 | $combined = (int) $value . (int) $appended_value; 57 | settype( $combined, 'integer' ); 58 | 59 | // Turn compression off 60 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_COMPRESSION, false ) ); 61 | 62 | // Add value 63 | $this->assertTrue( $this->object_cache->add( $key, $value ) ); 64 | 65 | // Verify add 66 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 67 | 68 | // Append 69 | $this->assertTrue( $this->object_cache->append( $key, $appended_value ) ); 70 | 71 | // Verify append 72 | $this->assertSame( $combined, $this->object_cache->get( $key ) ); 73 | $this->assertSame( $combined, $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 74 | 75 | // Turn compression back on 76 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_COMPRESSION, true ) ); 77 | } 78 | 79 | public function test_append_array_should_fail() { 80 | $key = microtime(); 81 | 82 | $value = array( 'gretzky', 'messier' ); 83 | $appended_value = array( 'kuri', 'fuhr' ); 84 | 85 | // Add value 86 | $this->assertTrue( $this->object_cache->add( $key, $value ) ); 87 | 88 | // Verify add 89 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 90 | 91 | // Append, which should fail due to data type 92 | $this->assertFalse( $this->object_cache->append( $key, $appended_value ) ); 93 | } 94 | 95 | /** 96 | * @expectedException PHPUnit_Framework_Error 97 | */ 98 | public function test_append_by_key_fails_with_compression_on() { 99 | $key = microtime(); 100 | 101 | $value = 'jordan'; 102 | $appended_value = 'pippen'; 103 | 104 | $server_key = 'bulls'; 105 | 106 | // Add value 107 | $this->assertTrue( $this->object_cache->addByKey( $key, $value ) ); 108 | 109 | // Verify add 110 | $this->assertSame( $value, $this->object_cache->getByKey( $key ) ); 111 | 112 | // Append should fail because compression is on by default 113 | // Note, this should throw an exception, not return false 114 | $this->assertFalse( $this->object_cache->appendByKey( $server_key, $appended_value, $key ) ); 115 | } 116 | 117 | public function test_append_by_key_string_with_compression_off() { 118 | $key = microtime(); 119 | 120 | $value = 'jordan'; 121 | $appended_value = 'pippen'; 122 | $combined = $value . $appended_value; 123 | 124 | $server_key = 'bulls'; 125 | 126 | // Turn compression off 127 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_COMPRESSION, false ) ); 128 | 129 | // Add value 130 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value ) ); 131 | 132 | // Verify add 133 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 134 | 135 | // Append 136 | $this->assertTrue( $this->object_cache->appendByKey( $server_key, $key, $appended_value ) ); 137 | 138 | // Verify append 139 | $this->assertSame( $combined, $this->object_cache->getByKey( $server_key, $key ) ); 140 | 141 | // Turn compression back on 142 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_COMPRESSION, true ) ); 143 | } 144 | 145 | public function test_append_by_key_int_is_casted_to_string() { 146 | $key = microtime(); 147 | 148 | $value = 23; 149 | $appended_value = 45.42; 150 | $combined = (int) $value . (int) $appended_value; 151 | settype( $combined, 'integer' ); 152 | 153 | $server_key = 'chicago'; 154 | 155 | // Turn compression off 156 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_COMPRESSION, false ) ); 157 | 158 | // Add value 159 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value ) ); 160 | 161 | // Verify add 162 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 163 | 164 | // Append 165 | $this->assertTrue( $this->object_cache->appendByKey( $server_key, $key, $appended_value ) ); 166 | 167 | // Verify append 168 | $this->assertSame( $combined, $this->object_cache->getByKey( $server_key, $key ) ); 169 | $this->assertSame( $combined, $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 170 | 171 | // Turn compression back on 172 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_COMPRESSION, true ) ); 173 | } 174 | 175 | public function test_append_array_by_key_should_fail() { 176 | $key = microtime(); 177 | 178 | $value = array( 'gretzky', 'messier' ); 179 | $appended_value = array( 'kuri', 'fuhr' ); 180 | 181 | $server_key = 'blackhawks'; 182 | 183 | // Add value 184 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value ) ); 185 | 186 | // Verify add 187 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 188 | 189 | // Append, which should fail due to data type 190 | $this->assertFalse( $this->object_cache->appendByKey( $server_key, $key, $appended_value ) ); 191 | } 192 | } -------------------------------------------------------------------------------- /tests/tests/other.php: -------------------------------------------------------------------------------- 1 | assertTrue( $this->object_cache->addServer( $servers[0][0], $servers[0][1], $servers[0][2] ) ); 9 | } 10 | 11 | public function test_add_servers() { 12 | $servers = array( array( '127.0.0.1', 11211, 1 ), array( '127.0.0.1', 11212, 1 ) ); 13 | 14 | // Add server 15 | $this->assertTrue( $this->object_cache->addServers( $servers ) ); 16 | } 17 | 18 | public function test_flush() { 19 | $key = microtime(); 20 | $value = 'brodeur'; 21 | 22 | // Add to memcached 23 | $this->assertTrue( $this->object_cache->add( $key, $value ) ); 24 | 25 | // Verify correct value and type is returned 26 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 27 | 28 | // Flush cache 29 | $this->assertTrue( $this->object_cache->flush() ); 30 | 31 | // Make sure value is no longer available 32 | $this->assertFalse( $this->object_cache->get( $key ) ); 33 | } 34 | 35 | public function test_get_all_options() { 36 | $this->assertContainsOnly( 'boolean', array( $this->object_cache->getOption( Memcached::OPT_COMPRESSION ) ) ); 37 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_SERIALIZER ) ) ); 38 | $this->assertContainsOnly( 'string', array( $this->object_cache->getOption( Memcached::OPT_PREFIX_KEY ) ) ); 39 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_HASH ) ) ); 40 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_DISTRIBUTION ) ) ); 41 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_LIBKETAMA_COMPATIBLE ) ) ); 42 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_BUFFER_WRITES ) ) ); 43 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_BINARY_PROTOCOL ) ) ); 44 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_NO_BLOCK ) ) ); 45 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_TCP_NODELAY ) ) ); 46 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_SOCKET_SEND_SIZE ) ) ); 47 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_SOCKET_RECV_SIZE ) ) ); 48 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_CONNECT_TIMEOUT ) ) ); 49 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_RETRY_TIMEOUT ) ) ); 50 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_SEND_TIMEOUT ) ) ); 51 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_POLL_TIMEOUT ) ) ); 52 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_RECV_TIMEOUT ) ) ); 53 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_SERVER_FAILURE_LIMIT ) ) ); 54 | $this->assertContainsOnly( 'int', array( $this->object_cache->getOption( Memcached::OPT_CACHE_LOOKUPS ) ) ); 55 | } 56 | 57 | public function test_get_result_code_returns_int() { 58 | $key = microtime(); 59 | $value = 'test'; 60 | 61 | $this->assertTrue( $this->object_cache->set( $key, $value ) ); 62 | 63 | $this->assertContainsOnly( 'int', array( $this->object_cache->getResultCode() ) ); 64 | 65 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 66 | 67 | $this->assertContainsOnly( 'int', array( $this->object_cache->getResultCode() ) ); 68 | } 69 | 70 | public function test_get_result_message_returns_string() { 71 | $key = microtime(); 72 | $value = 'test'; 73 | 74 | $this->assertTrue( $this->object_cache->set( $key, $value ) ); 75 | 76 | $this->assertContainsOnly( 'string', array( $this->object_cache->getResultMessage() ) ); 77 | 78 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 79 | 80 | $this->assertContainsOnly( 'string', array( $this->object_cache->getResultMessage() ) ); 81 | } 82 | 83 | public function test_get_server_by_key() { 84 | $key = microtime(); 85 | $value = 'mwanga'; 86 | $server_key = 'perkins'; 87 | 88 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value ) ); 89 | 90 | $this->assertContainsOnly( 'array', array( $this->object_cache->getServerByKey( $server_key ) ) ); 91 | } 92 | 93 | public function test_get_server_list() { 94 | $this->assertContainsOnly( 'array', array( $this->object_cache->getServerList() ) ); 95 | } 96 | 97 | public function test_get_stats() { 98 | // I'm getting a weird failure to get stats whenever the Linode server is used. Account for this here. 99 | if ( ! array_key_exists( 'linode', $this->servers ) ) 100 | $this->assertTrue( is_array( $this->object_cache->getStats() ) ); 101 | } 102 | 103 | public function test_get_version() { 104 | $this->assertTrue( is_array( $this->object_cache->getVersion() ) ); 105 | } 106 | 107 | public function test_set_option() { 108 | $value = 'widgets'; 109 | 110 | $this->assertTrue( $this->object_cache->setOption( Memcached::OPT_PREFIX_KEY, $value ) ); 111 | $this->assertSame( $value, $this->object_cache->getOption( Memcached::OPT_PREFIX_KEY ) ); 112 | } 113 | 114 | public function test_switch_to_blog() { 115 | $key = 'oshie'; 116 | $val = 'kovalchuk'; 117 | $val2 = 'bobrovsky'; 118 | 119 | if ( ! is_multisite() ) { 120 | // Single site ingnores switch_to_blog(). 121 | $this->assertTrue( $this->object_cache->set( $key, $val ) ); 122 | $this->assertEquals( $val, $this->object_cache->get( $key ) ); 123 | $this->object_cache->switch_to_blog( 999 ); 124 | $this->assertEquals( $val, $this->object_cache->get( $key ) ); 125 | $this->assertTrue( $this->object_cache->set( $key, $val2 ) ); 126 | $this->assertEquals( $val2, $this->object_cache->get( $key ) ); 127 | $this->object_cache->switch_to_blog( get_current_blog_id() ); 128 | $this->assertEquals( $val2, $this->object_cache->get( $key ) ); 129 | } else { 130 | // Multisite should have separate per-blog caches 131 | $this->assertTrue( $this->object_cache->set( $key, $val ) ); 132 | $this->assertEquals( $val, $this->object_cache->get( $key ) ); 133 | $this->object_cache->switch_to_blog( 999 ); 134 | $this->assertFalse( $this->object_cache->get( $key ) ); 135 | $this->assertTrue( $this->object_cache->set( $key, $val2 ) ); 136 | $this->assertEquals( $val2, $this->object_cache->get( $key ) ); 137 | $this->object_cache->switch_to_blog( get_current_blog_id() ); 138 | $this->assertEquals( $val, $this->object_cache->get( $key ) ); 139 | $this->object_cache->switch_to_blog( 999 ); 140 | $this->assertEquals( $val2, $this->object_cache->get( $key ) ); 141 | $this->object_cache->switch_to_blog( get_current_blog_id() ); 142 | $this->assertEquals( $val, $this->object_cache->get( $key ) ); 143 | } 144 | 145 | // Global group 146 | $this->object_cache->add_global_groups( 'global-cache-test' ); 147 | $this->assertTrue( $this->object_cache->set( $key, $val, 'global-cache-test' ) ); 148 | $this->assertEquals( $val, $this->object_cache->get( $key, 'global-cache-test' ) ); 149 | $this->object_cache->switch_to_blog( 999 ); 150 | $this->assertEquals( $val, $this->object_cache->get( $key, 'global-cache-test' ) ); 151 | $this->assertTrue( $this->object_cache->set( $key, $val2, 'global-cache-test' ) ); 152 | $this->assertEquals( $val2, $this->object_cache->get( $key, 'global-cache-test' ) ); 153 | $this->object_cache->switch_to_blog( get_current_blog_id() ); 154 | $this->assertEquals( $val2, $this->object_cache->get( $key, 'global-cache-test' ) ); 155 | } 156 | 157 | public function test_sanitize_expiration_leaves_value_untouched_if_less_than_thirty_days() { 158 | $time = 5; 159 | $this->assertEquals( $time, $this->object_cache->sanitize_expiration( $time ) ); 160 | } 161 | 162 | public function test_sanitize_expiration_leaves_value_untouched_if_exactly_thirty_days() { 163 | $time = 60 * 60 * 24 * 30; 164 | $this->assertEquals( $time, $this->object_cache->sanitize_expiration( $time ) ); 165 | } 166 | 167 | public function test_sanitize_expiration_should_adjust_expiration_if_later_than_now() { 168 | $time = 60 * 60 * 24 * 31; 169 | $now = time(); 170 | 171 | // We need to manually set the internal timer to make sure we get the right value in testing 172 | $this->object_cache->now = $now; 173 | 174 | $this->assertEquals( $time + $now, $this->object_cache->sanitize_expiration( $time ) ); 175 | } 176 | } -------------------------------------------------------------------------------- /tests/tests/add.php: -------------------------------------------------------------------------------- 1 | assertTrue( $this->object_cache->add( $key, $value ) ); 13 | 14 | // Verify correct value is returned 15 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 16 | } 17 | 18 | /** 19 | * Verify "add" method with int as value 20 | */ 21 | public function test_add_int() { 22 | $key = microtime(); 23 | $value = 42; 24 | 25 | // Add int to memcached 26 | $this->assertTrue( $this->object_cache->add( $key, $value ) ); 27 | 28 | // Verify correct value and type is returned 29 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 30 | } 31 | 32 | /** 33 | * Verify "add" method with array as value 34 | */ 35 | public function test_add_array() { 36 | $key = microtime(); 37 | $value = array( 5, 'quick' ); 38 | 39 | // Add array to memcached 40 | $this->assertTrue( $this->object_cache->add( $key, $value ) ); 41 | 42 | // Verify correct value and type is returned 43 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 44 | } 45 | 46 | /** 47 | * Verify "add" method values when adding second object with existing key 48 | */ 49 | public function test_add_fails_if_key_exists() { 50 | $key = microtime(); 51 | $value1 = 'parise'; 52 | $value2 = 'king'; 53 | 54 | // Verify that one value is added to cache 55 | $this->assertTrue( $this->object_cache->add( $key, $value1 ) ); 56 | 57 | // Make sure second value with same key fails 58 | $this->assertFalse( $this->object_cache->add( $key, $value2 ) ); 59 | 60 | // Make sure the value of the key is still correct 61 | $this->assertSame( $value1, $this->object_cache->get( $key ) ); 62 | } 63 | 64 | /** 65 | * Verify "add" method stores a no_mc_group in 66 | */ 67 | public function test_add_avoid_memcached_if_no_mc_group() { 68 | $key = microtime(); 69 | $group = 'comment'; 70 | $value = 'brown'; 71 | 72 | // Verify that the data is added 73 | $this->assertTrue( $this->object_cache->add( $key, $value, $group ) ); 74 | 75 | // Verify that the data is in the runtime cache 76 | $this->assertSame( $value, $this->object_cache->cache[ $this->object_cache->buildKey( $key, $group ) ] ); 77 | 78 | // Verify that the data is accessible by the get method 79 | $this->assertSame( $value, $this->object_cache->get( $key, $group ) ); 80 | 81 | // Verify that the data is not in memcached by making a direct request 82 | $this->assertFalse( $this->object_cache->m->get( $this->object_cache->buildKey( $key, $group ) ) ); 83 | } 84 | 85 | /** 86 | * Verify "addByKey" method with string as value 87 | */ 88 | public function test_add_by_key_string() { 89 | $key = microtime(); 90 | $value = 'kovalchuk'; 91 | $server_key_real = 'doughty'; 92 | 93 | // Add string to memcached 94 | $this->assertTrue( $this->object_cache->addByKey( $server_key_real, $key, $value ) ); 95 | 96 | // Verify correct value/type is returned 97 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key_real, $key ) ); 98 | } 99 | 100 | /** 101 | * Verify "addByKey" method with int as value 102 | */ 103 | public function test_add_by_key_int() { 104 | $key = microtime(); 105 | $value = 42; 106 | $server_key_real = 'doughty'; 107 | 108 | // Add int to memcached 109 | $this->assertTrue( $this->object_cache->addByKey( $server_key_real, $key, $value ) ); 110 | 111 | // Verify correct value/type is returned 112 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key_real, $key ) ); 113 | } 114 | 115 | /** 116 | * Verify "addByKey" method with array as value 117 | */ 118 | public function test_add_by_key_array() { 119 | $key = microtime(); 120 | $value = array( 5, 'value' ); 121 | $server_key_real = 'doughty'; 122 | 123 | // Add array to memcached 124 | $this->assertTrue( $this->object_cache->addByKey( $server_key_real, $key, $value ) ); 125 | 126 | // Verify correct value/type is returned 127 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key_real, $key ) ); 128 | } 129 | 130 | /** 131 | * Verify "addByKey" method values when adding second object with existing key 132 | */ 133 | public function test_add_by_key_fails_if_key_exists() { 134 | $key = microtime(); 135 | 136 | $value1 = 'stevens'; 137 | $value2 = 'kuri'; 138 | 139 | $server_key_real = 'doughty'; 140 | $server_key_fake = 'peppers'; 141 | 142 | // Verify that one value is added to cache 143 | $this->assertTrue( $this->object_cache->addByKey( $server_key_real, $key, $value1 ) ); 144 | 145 | // Make sure second value with same key fails 146 | $this->assertFalse( $this->object_cache->addByKey( $server_key_real, $key, $value2 ) ); 147 | 148 | // Make sure second value with different server key fails 149 | $this->assertFalse( $this->object_cache->addByKey( $server_key_fake, $key, $value2 ) ); 150 | 151 | // Make sure the value of the key is still correct 152 | $this->assertSame( $value1, $this->object_cache->getByKey( $server_key_real, $key ) ); 153 | } 154 | 155 | /** 156 | * Verify "addByKey" method stores a no_mc_group in 157 | */ 158 | public function test_add_by_key_avoid_memcached_if_no_mc_group() { 159 | $key = microtime(); 160 | 161 | $value1 = 'stevens'; 162 | 163 | $server_key_real = 'doughty'; 164 | 165 | $group = 'comment'; 166 | 167 | // Verify that the data is added 168 | $this->assertTrue( $this->object_cache->addByKey( $server_key_real, $key, $value1, $group ) ); 169 | 170 | // Verify that the data is in the runtime cache 171 | $this->assertSame( $value1, $this->object_cache->cache[ $this->object_cache->buildKey( $key, $group ) ] ); 172 | 173 | // Verify that the data is accessible by the get method 174 | $this->assertSame( $value1, $this->object_cache->getByKey( $server_key_real, $key, $group ) ); 175 | 176 | // Verify that the data is not in memcached by making a direct request 177 | $this->assertFalse( $this->object_cache->m->get( $this->object_cache->buildKey( $key, $group ) ) ); 178 | } 179 | 180 | /** 181 | * Verify that wp_suspend_cache_addition() stops items from being added to cache 182 | */ 183 | public function test_add_suspended_by_wp_cache_suspend_addition_string() { 184 | $key = microtime(); 185 | $value = 'crawford'; 186 | 187 | // Suspend the cache 188 | wp_suspend_cache_addition( true ); 189 | 190 | // Attempt to add string to cache 191 | $this->assertFalse( $this->object_cache->add( $key, $value ) ); 192 | 193 | // Verify that the value does not exist in cache 194 | $this->object_cache->get( $key ); 195 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 196 | } 197 | 198 | /** 199 | * Verify that wp_suspend_cache_addition() stops items from being added to cache, but allows additions after re-enabled 200 | */ 201 | public function test_add_enabled_by_wp_cache_un_suspend_addition_string() { 202 | $key = microtime(); 203 | $value = 'miller'; 204 | 205 | // Suspend the cache 206 | wp_suspend_cache_addition( true ); 207 | 208 | // Attempt to add string to cache 209 | $this->assertFalse( $this->object_cache->add( $key, $value ) ); 210 | 211 | // Verify that the value does not exist in cache 212 | $this->object_cache->get( $key ); 213 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 214 | 215 | $key = microtime(); 216 | $value = 'carruth'; 217 | 218 | // Re-enable the cache 219 | wp_suspend_cache_addition( false ); 220 | 221 | // Add the string to the cache 222 | $this->assertTrue( $this->object_cache->add( $key, $value ) ); 223 | 224 | // Verify that the value is in the cache 225 | $this->assertSame( $value, $this->object_cache->get( $key )); 226 | } 227 | 228 | public function test_add_with_expiration_of_30_days() { 229 | $key = 'usa'; 230 | $value = 'merica'; 231 | $group = 'july'; 232 | $built_key = $this->object_cache->buildKey( $key, $group ); 233 | 234 | // 30 days 235 | $expiration = 60 * 60 * 24 * 30; 236 | 237 | $this->assertTrue( $this->object_cache->add( $key, $value, $group, $expiration ) ); 238 | 239 | // Verify that the value is in cache by accessing memcached directly 240 | $this->assertEquals( $value, $this->object_cache->m->get( $built_key ) ); 241 | 242 | // Remove the value from internal cache to force a lookup 243 | unset( $this->object_cache->cache[ $built_key ] ); 244 | 245 | // Verify that the value is no longer in the internal cache 246 | $this->assertFalse( $this->object_cache->get_from_runtime_cache( $key, $group ) ); 247 | 248 | // Do the lookup with the API to verify that we get the value 249 | $this->assertEquals( $value, $this->object_cache->get( $key, $group ) ); 250 | } 251 | 252 | public function test_add_with_expiration_longer_than_30_days() { 253 | $key = 'usa'; 254 | $value = 'merica'; 255 | $group = 'july'; 256 | $built_key = $this->object_cache->buildKey( $key, $group ); 257 | 258 | // 30 days and 1 second; if interpreted as timestamp, becomes "Sat, 31 Jan 1970 00:00:01 GMT" 259 | $expiration = 60 * 60 * 24 * 30 + 1; 260 | 261 | $this->assertTrue( $this->object_cache->add( $key, $value, $group, $expiration ) ); 262 | 263 | // Verify that the value is in cache 264 | $this->assertEquals( $value, $this->object_cache->m->get( $built_key ) ); 265 | 266 | // Remove the value from internal cache to force a lookup 267 | unset( $this->object_cache->cache[ $built_key ] ); 268 | 269 | // Verify that the value is no longer in the internal cache 270 | $this->assertFalse( $this->object_cache->get_from_runtime_cache( $key, $group ) ); 271 | 272 | // Do the lookup with the API to verify that we get the value 273 | $this->assertEquals( $value, $this->object_cache->get( $key, $group ) ); 274 | } 275 | } -------------------------------------------------------------------------------- /tests/tests/set.php: -------------------------------------------------------------------------------- 1 | assertTrue( $this->object_cache->set( $key, $value ) ); 10 | 11 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 12 | 13 | $this->assertSame( $value, $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 14 | } 15 | 16 | public function test_set_value_with_expiration() { 17 | $key = microtime(); 18 | 19 | $value = 'ck'; 20 | 21 | $this->assertTrue( $this->object_cache->set( $key, $value, 'default', 3600 ) ); 22 | 23 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 24 | 25 | $this->assertSame( $value, $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 26 | } 27 | 28 | public function test_set_value_overwrites_previous() { 29 | $key = microtime(); 30 | 31 | $value = 'ck'; 32 | $new_value = 'abc'; 33 | 34 | $this->assertTrue( $this->object_cache->set( $key, $value ) ); 35 | 36 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 37 | 38 | $this->assertSame( $value, $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 39 | 40 | $this->assertTrue( $this->object_cache->set( $key, $new_value ) ); 41 | 42 | $this->assertSame( $new_value, $this->object_cache->get( $key ) ); 43 | 44 | $this->assertSame( $new_value, $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 45 | } 46 | 47 | public function test_set_value_group() { 48 | $key = microtime(); 49 | 50 | $value = 'ck'; 51 | $group = 'hola'; 52 | 53 | $this->assertTrue( $this->object_cache->set( $key, $value, $group ) ); 54 | 55 | $this->assertSame( $value, $this->object_cache->get( $key, $group ) ); 56 | 57 | $this->assertSame( $value, $this->object_cache->cache[ $this->object_cache->buildKey( $key, $group ) ] ); 58 | } 59 | 60 | public function test_set_value_no_mc_group() { 61 | $key = microtime(); 62 | 63 | $value = 'ck'; 64 | $group = 'counts'; 65 | 66 | $this->assertTrue( $this->object_cache->set( $key, $value, $group ) ); 67 | 68 | $this->assertSame( $value, $this->object_cache->get( $key, $group ) ); 69 | 70 | $this->assertFalse( $this->object_cache->m->get( $this->object_cache->buildKey( $key, $group ) ) ); 71 | 72 | $this->assertSame( $value, $this->object_cache->cache[ $this->object_cache->buildKey( $key, $group ) ] ); 73 | } 74 | 75 | public function test_set_by_key_value() { 76 | $key = microtime(); 77 | 78 | $value = 'ck'; 79 | 80 | $server_key = 'hbo'; 81 | 82 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value ) ); 83 | 84 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 85 | 86 | $this->assertSame( $value, $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 87 | } 88 | 89 | public function test_set_by_key_value_with_expiration() { 90 | $key = microtime(); 91 | 92 | $value = 'ck'; 93 | 94 | $server_key = 'hbo'; 95 | 96 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value, 'default', 3600 ) ); 97 | 98 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 99 | 100 | $this->assertSame( $value, $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 101 | } 102 | 103 | public function test_set_by_key_value_overwrites_previous() { 104 | $key = microtime(); 105 | 106 | $value = 'ck'; 107 | $new_value = 'abc'; 108 | 109 | $server_key = 'hbo'; 110 | 111 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value ) ); 112 | 113 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 114 | 115 | $this->assertSame( $value, $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 116 | 117 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $new_value ) ); 118 | 119 | $this->assertSame( $new_value, $this->object_cache->getByKey( $server_key, $key ) ); 120 | 121 | $this->assertSame( $new_value, $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 122 | } 123 | 124 | public function test_set_by_key_value_group() { 125 | $key = microtime(); 126 | 127 | $value = 'ck'; 128 | $group = 'hola'; 129 | 130 | $server_key = 'hbo'; 131 | 132 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value, $group ) ); 133 | 134 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, $group ) ); 135 | 136 | $this->assertSame( $value, $this->object_cache->cache[ $this->object_cache->buildKey( $key, $group ) ] ); 137 | } 138 | 139 | public function test_set_by_key_value_no_mc_group() { 140 | $key = microtime(); 141 | 142 | $value = 'ck'; 143 | $group = 'counts'; 144 | 145 | $server_key = 'hbo'; 146 | 147 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value, $group ) ); 148 | 149 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, $group ) ); 150 | 151 | $this->assertFalse( $this->object_cache->m->getByKey( $server_key, $this->object_cache->buildKey( $key, $group ) ) ); 152 | 153 | $this->assertSame( $value, $this->object_cache->cache[ $this->object_cache->buildKey( $key, $group ) ] ); 154 | } 155 | 156 | public function test_set_multi_sets_values() { 157 | $items = array( 158 | microtime() . '-tester' => 'howdy', 159 | microtime() . '-yoyo' => 'What is cracking', 160 | microtime() . '-hidely-ho' => 'ouch' 161 | ); 162 | 163 | $this->assertTrue( $this->object_cache->setMulti( $items ) ); 164 | 165 | foreach ( $items as $key => $value ) 166 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 167 | } 168 | 169 | public function test_set_multi_sets_values_with_different_groups() { 170 | $keys = array( 171 | microtime() . '-sharp' => array( 172 | 'value' => 10, 173 | 'group' => 'blackhawks' 174 | ), 175 | microtime() . '-toews' => array( 176 | 'value' => 'nineteen', 177 | 'group' => 'blackhawks' 178 | ), 179 | microtime() . '-crosby' => array( 180 | 'value' => '87', 181 | 'group' => 'pengiuns' 182 | ), 183 | microtime() . '-suter' => array( 184 | 'value' => 'twenty', 185 | 'group' => 'default' 186 | ), 187 | microtime() . '-bettman' => array( 188 | 'value' => 'commish' 189 | ) 190 | ); 191 | 192 | $rekeyed = array(); 193 | foreach ( $keys as $key => $value ) { 194 | if ( isset( $value['group'] ) ) 195 | $rekeyed[ $this->object_cache->buildKey( $key, $value['group'] ) ] = $value; 196 | else 197 | $rekeyed[ $this->object_cache->buildKey( $key ) ] = $value; 198 | } 199 | 200 | $items = array(); 201 | foreach ( $keys as $key => $value ) 202 | $items[$key] = $value['value']; 203 | 204 | $groups = array(); 205 | foreach ( $keys as $key => $value ) { 206 | if ( isset( $value['group'] ) ) 207 | $groups[] = $value['group']; 208 | } 209 | 210 | $this->assertTrue( $this->object_cache->setMulti( $items, $groups ) ); 211 | 212 | // Verify each value in memcached and internal cache 213 | foreach ( $keys as $key => $value ) { 214 | if ( isset( $value['group'] ) ) { 215 | $this->assertSame( $value['value'], $this->object_cache->cache[ $this->object_cache->buildKey( $key, $value['group'] ) ] ); 216 | $this->assertSame( $value['value'], $this->object_cache->get( $key, $value['group'] ) ); 217 | $this->assertSame( $value['value'], $this->object_cache->cache[ $this->object_cache->buildKey( $key, $value['group'] ) ] ); 218 | } else { 219 | $this->assertSame( $value['value'], $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 220 | $this->assertSame( $value['value'], $this->object_cache->get( $key ) ); 221 | $this->assertSame( $value['value'], $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 222 | } 223 | } 224 | } 225 | 226 | public function test_set_multi_sets_values_with_different_groups_including_no_mc_groups() { 227 | $keys = array( 228 | microtime() . '-sharp' => array( 229 | 'value' => 10, 230 | 'group' => 'blackhawks' 231 | ), 232 | microtime() . '-toews' => array( 233 | 'value' => 'nineteen', 234 | 'group' => 'comment' 235 | ), 236 | microtime() . '-crosby' => array( 237 | 'value' => '87', 238 | 'group' => 'counts' 239 | ), 240 | microtime() . '-suter' => array( 241 | 'value' => 'twenty', 242 | 'group' => 'default' 243 | ), 244 | microtime() . '-bettman' => array( 245 | 'value' => 'commish' 246 | ) 247 | ); 248 | 249 | $rekeyed = array(); 250 | foreach ( $keys as $key => $value ) { 251 | if ( isset( $value['group'] ) ) 252 | $rekeyed[ $this->object_cache->buildKey( $key, $value['group'] ) ] = $value; 253 | else 254 | $rekeyed[ $this->object_cache->buildKey( $key ) ] = $value; 255 | } 256 | 257 | $items = array(); 258 | foreach ( $keys as $key => $value ) 259 | $items[$key] = $value['value']; 260 | 261 | $groups = array(); 262 | foreach ( $keys as $key => $value ) { 263 | if ( isset( $value['group'] ) ) 264 | $groups[] = $value['group']; 265 | } 266 | 267 | $this->assertTrue( $this->object_cache->setMulti( $items, $groups ) ); 268 | 269 | // Verify each value in memcached and internal cache 270 | foreach ( $keys as $key => $value ) { 271 | if ( isset( $value['group'] ) ) { 272 | $this->assertSame( $value['value'], $this->object_cache->cache[ $this->object_cache->buildKey( $key, $value['group'] ) ] ); 273 | $this->assertSame( $value['value'], $this->object_cache->get( $key, $value['group'] ) ); 274 | $this->assertSame( $value['value'], $this->object_cache->cache[ $this->object_cache->buildKey( $key, $value['group'] ) ] ); 275 | } else { 276 | $this->assertSame( $value['value'], $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 277 | $this->assertSame( $value['value'], $this->object_cache->get( $key ) ); 278 | $this->assertSame( $value['value'], $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 279 | } 280 | } 281 | } 282 | 283 | public function test_set_multi_by_key_sets_values() { 284 | $items = array( 285 | microtime() . '-tester' => 'howdy', 286 | microtime() . '-yoyo' => 'What is cracking', 287 | microtime() . '-hidely-ho' => 'ouch' 288 | ); 289 | 290 | $server_key = 'anger-management'; 291 | 292 | $this->assertTrue( $this->object_cache->setMultiByKey( $server_key, $items ) ); 293 | 294 | foreach ( $items as $key => $value ) 295 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 296 | } 297 | 298 | public function test_set_multi_by_key_sets_values_with_different_groups() { 299 | $keys = array( 300 | microtime() . '-sharp' => array( 301 | 'value' => 10, 302 | 'group' => 'blackhawks' 303 | ), 304 | microtime() . '-toews' => array( 305 | 'value' => 'nineteen', 306 | 'group' => 'blackhawks' 307 | ), 308 | microtime() . '-crosby' => array( 309 | 'value' => '87', 310 | 'group' => 'pengiuns' 311 | ), 312 | microtime() . '-suter' => array( 313 | 'value' => 'twenty', 314 | 'group' => 'default' 315 | ), 316 | microtime() . '-bettman' => array( 317 | 'value' => 'commish' 318 | ) 319 | ); 320 | 321 | $server_key = 'anger-management'; 322 | 323 | $rekeyed = array(); 324 | foreach ( $keys as $key => $value ) { 325 | if ( isset( $value['group'] ) ) 326 | $rekeyed[ $this->object_cache->buildKey( $key, $value['group'] ) ] = $value; 327 | else 328 | $rekeyed[ $this->object_cache->buildKey( $key ) ] = $value; 329 | } 330 | 331 | $items = array(); 332 | foreach ( $keys as $key => $value ) 333 | $items[$key] = $value['value']; 334 | 335 | $groups = array(); 336 | foreach ( $keys as $key => $value ) { 337 | if ( isset( $value['group'] ) ) 338 | $groups[] = $value['group']; 339 | } 340 | 341 | $this->assertTrue( $this->object_cache->setMultiByKey( $server_key, $items, $groups ) ); 342 | 343 | // Verify each value in memcached and internal cache 344 | foreach ( $keys as $key => $value ) { 345 | if ( isset( $value['group'] ) ) { 346 | $this->assertSame( $value['value'], $this->object_cache->cache[ $this->object_cache->buildKey( $key, $value['group'] ) ] ); 347 | $this->assertSame( $value['value'], $this->object_cache->getByKey( $server_key, $key, $value['group'] ) ); 348 | $this->assertSame( $value['value'], $this->object_cache->cache[ $this->object_cache->buildKey( $key, $value['group'] ) ] ); 349 | } else { 350 | $this->assertSame( $value['value'], $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 351 | $this->assertSame( $value['value'], $this->object_cache->getByKey( $server_key, $key ) ); 352 | $this->assertSame( $value['value'], $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 353 | } 354 | } 355 | } 356 | 357 | public function test_set_multi_by_key_sets_values_with_different_groups_including_no_mc_groups() { 358 | $keys = array( 359 | microtime() . '-sharp' => array( 360 | 'value' => 10, 361 | 'group' => 'blackhawks' 362 | ), 363 | microtime() . '-toews' => array( 364 | 'value' => 'nineteen', 365 | 'group' => 'comment' 366 | ), 367 | microtime() . '-crosby' => array( 368 | 'value' => '87', 369 | 'group' => 'counts' 370 | ), 371 | microtime() . '-suter' => array( 372 | 'value' => 'twenty', 373 | 'group' => 'default' 374 | ), 375 | microtime() . '-bettman' => array( 376 | 'value' => 'commish' 377 | ) 378 | ); 379 | 380 | $server_key = 'anger-management'; 381 | 382 | $rekeyed = array(); 383 | foreach ( $keys as $key => $value ) { 384 | if ( isset( $value['group'] ) ) 385 | $rekeyed[ $this->object_cache->buildKey( $key, $value['group'] ) ] = $value; 386 | else 387 | $rekeyed[ $this->object_cache->buildKey( $key ) ] = $value; 388 | } 389 | 390 | $items = array(); 391 | foreach ( $keys as $key => $value ) 392 | $items[$key] = $value['value']; 393 | 394 | $groups = array(); 395 | foreach ( $keys as $key => $value ) { 396 | if ( isset( $value['group'] ) ) 397 | $groups[] = $value['group']; 398 | } 399 | 400 | $this->assertTrue( $this->object_cache->setMultiByKey( $server_key, $items, $groups ) ); 401 | 402 | // Verify each value in memcached and internal cache 403 | foreach ( $keys as $key => $value ) { 404 | if ( isset( $value['group'] ) ) { 405 | $this->assertSame( $value['value'], $this->object_cache->cache[ $this->object_cache->buildKey( $key, $value['group'] ) ] ); 406 | $this->assertSame( $value['value'], $this->object_cache->getByKey( $server_key, $key, $value['group'] ) ); 407 | $this->assertSame( $value['value'], $this->object_cache->cache[ $this->object_cache->buildKey( $key, $value['group'] ) ] ); 408 | } else { 409 | $this->assertSame( $value['value'], $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 410 | $this->assertSame( $value['value'], $this->object_cache->getByKey( $server_key, $key ) ); 411 | $this->assertSame( $value['value'], $this->object_cache->cache[ $this->object_cache->buildKey( $key ) ] ); 412 | } 413 | } 414 | } 415 | 416 | public function test_set_with_expiration_of_30_days() { 417 | $key = 'usa'; 418 | $value = 'merica'; 419 | $group = 'july'; 420 | $built_key = $this->object_cache->buildKey( $key, $group ); 421 | 422 | // 30 days 423 | $expiration = 60 * 60 * 24 * 30; 424 | 425 | $this->assertTrue( $this->object_cache->set( $key, $value, $group, $expiration ) ); 426 | 427 | // Verify that the value is in cache by accessing memcached directly 428 | $this->assertEquals( $value, $this->object_cache->m->get( $built_key ) ); 429 | 430 | // Remove the value from internal cache to force a lookup 431 | unset( $this->object_cache->cache[ $built_key ] ); 432 | 433 | // Verify that the value is no longer in the internal cache 434 | $this->assertFalse( $this->object_cache->get_from_runtime_cache( $key, $group ) ); 435 | 436 | // Do the lookup with the API to verify that we get the value 437 | $this->assertEquals( $value, $this->object_cache->get( $key, $group ) ); 438 | } 439 | 440 | public function test_set_with_expiration_longer_than_30_days() { 441 | $key = 'usa'; 442 | $value = 'merica'; 443 | $group = 'july'; 444 | $built_key = $this->object_cache->buildKey( $key, $group ); 445 | 446 | // 30 days and 1 second; if interpreted as timestamp, becomes "Sat, 31 Jan 1970 00:00:01 GMT" 447 | $expiration = 60 * 60 * 24 * 30 + 1; 448 | 449 | $this->assertTrue( $this->object_cache->set( $key, $value, $group, $expiration ) ); 450 | 451 | // Verify that the value is in cache 452 | $this->assertEquals( $value, $this->object_cache->m->get( $built_key ) ); 453 | 454 | // Remove the value from internal cache to force a lookup 455 | unset( $this->object_cache->cache[ $built_key ] ); 456 | 457 | // Verify that the value is no longer in the internal cache 458 | $this->assertFalse( $this->object_cache->get_from_runtime_cache( $key, $group ) ); 459 | 460 | // Do the lookup with the API to verify that we get the value 461 | $this->assertEquals( $value, $this->object_cache->get( $key, $group ) ); 462 | } 463 | 464 | public function test_set_multi_with_expiration_of_30_days() { 465 | $values = array( 466 | 'header' => 'footer', 467 | 'goal' => 'save', 468 | 'fast' => 'slow', 469 | ); 470 | 471 | $group = 'CRC'; 472 | 473 | // 30 days 474 | $expiration = 60 * 60 * 24 * 30; 475 | 476 | $this->assertTrue( $this->object_cache->setMulti( $values, $group, $expiration ) ); 477 | 478 | // Verify values independently 479 | foreach ( $values as $key => $value ) { 480 | $built_key = $this->object_cache->buildKey( $key, $group ); 481 | 482 | // Verify that the value is in cache by accessing memcached directly 483 | $this->assertSame( $value, $this->object_cache->m->get( $built_key ) ); 484 | 485 | // Remove the value from internal cache to force a lookup 486 | unset( $this->object_cache->cache[ $built_key ] ); 487 | 488 | // Verify that the value is no longer in the internal cache 489 | $this->assertFalse( $this->object_cache->get_from_runtime_cache( $key, $group ) ); 490 | 491 | // Do the lookup with the API to verify that we get the value 492 | $this->assertEquals( $value, $this->object_cache->get( $key, $group ) ); 493 | } 494 | } 495 | 496 | public function test_set_multi_with_expiration_longer_than_30_days() { 497 | $values = array( 498 | 'header' => 'footer', 499 | 'goal' => 'save', 500 | 'fast' => 'slow', 501 | ); 502 | 503 | $group = 'CRC'; 504 | 505 | // 30 days and 1 second; if interpreted as timestamp, becomes "Sat, 31 Jan 1970 00:00:01 GMT" 506 | $expiration = 60 * 60 * 24 * 30 + 1; 507 | 508 | $this->assertTrue( $this->object_cache->setMulti( $values, $group, $expiration ) ); 509 | 510 | // Verify values independently 511 | foreach ( $values as $key => $value ) { 512 | $built_key = $this->object_cache->buildKey( $key, $group ); 513 | 514 | // Verify that the value is in cache by accessing memcached directly 515 | $this->assertSame( $value, $this->object_cache->m->get( $built_key ) ); 516 | 517 | // Remove the value from internal cache to force a lookup 518 | unset( $this->object_cache->cache[ $built_key ] ); 519 | 520 | // Verify that the value is no longer in the internal cache 521 | $this->assertFalse( $this->object_cache->get_from_runtime_cache( $key, $group ) ); 522 | 523 | // Do the lookup with the API to verify that we get the value 524 | $this->assertEquals( $value, $this->object_cache->get( $key, $group ) ); 525 | } 526 | } 527 | } -------------------------------------------------------------------------------- /tests/tests/get-multi.php: -------------------------------------------------------------------------------- 1 | 'crosby', 8 | 99 => 'gretzky', 9 | '68' => 'jagr' 10 | ); 11 | 12 | $keyed_values = array(); 13 | foreach ( $values as $key => $value ) 14 | $keyed_values[ $this->object_cache->buildKey( $key, 'default' ) ] = $value; 15 | 16 | // Add each key individually 17 | foreach ( $values as $key => $value ) 18 | $this->assertTrue( $this->object_cache->set( $key, $value ) ); 19 | 20 | // Test that return is same as input 21 | $this->assertEmpty( array_diff( $keyed_values, $this->object_cache->getMulti( array_keys( $values ) ) ) ); 22 | 23 | // Test that return is same as input with same order 24 | $this->assertSame( $keyed_values, $this->object_cache->getMulti( array_keys( $values ), 'default', '', $cas_tokens, Memcached::GET_PRESERVE_ORDER ) ); 25 | } 26 | 27 | public function test_get_multi_gets_multiple_values_if_added_individually_with_different_groups() { 28 | $values = array( 29 | 87 => array( 30 | 'value' => 'crosby', 31 | 'group' => 'penguins' 32 | ), 33 | 99 => array( 34 | 'value' => 'gretzky', 35 | 'group' => 'oilers' 36 | ), 37 | '68' => array( 38 | 'value' => 'jagr' 39 | ) 40 | ); 41 | 42 | $groups = array(); 43 | foreach ( $values as $key => $value ) { 44 | if ( isset( $value['group'] ) ) 45 | $groups[] = $value['group']; 46 | } 47 | 48 | $keyed_values = array(); 49 | foreach ( $values as $key => $value ) { 50 | if ( isset( $value['group'] ) ) 51 | $keyed_values[ $this->object_cache->buildKey( $key, $value['group'] ) ] = $value['value']; 52 | else 53 | $keyed_values[ $this->object_cache->buildKey( $key, 'default' ) ] = $value['value']; 54 | } 55 | 56 | // Add each key individually 57 | foreach ( $values as $key => $value ) { 58 | if ( isset( $value['group'] ) ) 59 | $this->assertTrue( $this->object_cache->set( $key, $value['value'], $value['group'] ) ); 60 | else 61 | $this->assertTrue( $this->object_cache->set( $key, $value['value'] ) ); 62 | } 63 | 64 | // Test that return is same as input 65 | $this->assertEmpty( array_diff( $keyed_values, $this->object_cache->getMulti( array_keys( $values ), $groups ) ) ); 66 | 67 | // Test for same order with flag set 68 | $this->assertSame( $keyed_values, $this->object_cache->getMulti( array_keys( $values ), $groups, '', $cas_tokens, Memcached::GET_PRESERVE_ORDER ) ); 69 | } 70 | 71 | public function test_get_multi_gets_multiple_values_if_added_individually_with_different_groups_including_no_mc_groups() { 72 | $values = array( 73 | 87 => array( 74 | 'value' => 'crosby', 75 | 'group' => 'penguins' 76 | ), 77 | 99 => array( 78 | 'value' => 'gretzky', 79 | 'group' => 'oilers' 80 | ), 81 | '68' => array( 82 | 'value' => 'jagr' 83 | ), 84 | // If these values are changed, be sure to change hardcoded versions below 85 | 'the-one-with-no-name' => array( 86 | 'value' => 'bettman', 87 | 'group' => 'counts' 88 | ) 89 | ); 90 | 91 | $groups = array(); 92 | foreach ( $values as $key => $value ) { 93 | if ( isset( $value['group'] ) ) 94 | $groups[] = $value['group']; 95 | else 96 | $groups[] = 'default'; 97 | } 98 | 99 | $keyed_values = array(); 100 | foreach ( $values as $key => $value ) { 101 | if ( isset( $value['group'] ) ) 102 | $keyed_values[ $this->object_cache->buildKey( $key, $value['group'] ) ] = $value['value']; 103 | else 104 | $keyed_values[ $this->object_cache->buildKey( $key, 'default' ) ] = $value['value']; 105 | } 106 | 107 | // Add each key individually 108 | foreach ( $values as $key => $value ) { 109 | if ( isset( $value['group'] ) ) 110 | $this->assertTrue( $this->object_cache->set( $key, $value['value'], $value['group'] ) ); 111 | else 112 | $this->assertTrue( $this->object_cache->set( $key, $value['value'] ) ); 113 | } 114 | 115 | // Test that return is same as input 116 | $this->assertSame( $keyed_values, $this->object_cache->getMulti( array_keys( $values ), $groups ) ); 117 | 118 | // Make sure that the no_mc_group value never made it to memcached 119 | $this->assertFalse( $this->object_cache->m->get( $this->object_cache->buildKey( 'the-one-with-no-name', 'counts' ) ) ); 120 | 121 | // Verify the no_mc_group value is in the internal cache 122 | $this->assertSame( 'bettman', $this->object_cache->cache[ $this->object_cache->buildKey( 'the-one-with-no-name', 'counts' ) ] ); 123 | } 124 | 125 | public function test_get_multi_gets_multiple_values_if_some_are_not_in_internal_cache() { 126 | $values = array( 127 | 87 => 'crosby', 128 | 99 => 'gretzky', 129 | '68' => 'jagr' 130 | ); 131 | 132 | $keyed_values = array(); 133 | foreach ( $values as $key => $value ) 134 | $keyed_values[ $this->object_cache->buildKey( $key, 'default' ) ] = $value; 135 | 136 | // Add each key individually 137 | foreach ( $values as $key => $value ) 138 | $this->assertTrue( $this->object_cache->set( $key, $value ) ); 139 | 140 | // Remove a value from internal cache 141 | $this->assertSame( 'gretzky', $this->object_cache->cache[ $this->object_cache->buildKey( 99 ) ] ); 142 | unset( $this->object_cache->cache[ $this->object_cache->buildKey( 99 ) ] ); 143 | $this->assertFalse( isset( $this->object_cache->cache[ $this->object_cache->buildKey( 99 ) ] ) ); 144 | 145 | // Test that the right values are returned is the wrong order without the flag set 146 | $this->assertEmpty( array_diff( $keyed_values, $this->object_cache->getMulti( array_keys( $values ) ) ) ); 147 | 148 | // Test that return is same as input 149 | $this->assertSame( $keyed_values, $this->object_cache->getMulti( array_keys( $values ), 'default', '', $cas_tokens, Memcached::GET_PRESERVE_ORDER ) ); 150 | } 151 | 152 | public function test_get_multi_order_if_added_individually_with_different_groups_including_no_mc_groups_and_if_some_are_not_in_internal_cache() { 153 | $values = array( 154 | 87 => array( 155 | 'value' => 'crosby', 156 | 'group' => 'penguins' 157 | ), 158 | 99 => array( 159 | 'value' => 'gretzky', 160 | 'group' => 'oilers' 161 | ), 162 | '68' => array( 163 | 'value' => 'jagr' 164 | ), 165 | // If these values are changed, be sure to change hardcoded versions below 166 | 'the-one-with-no-name' => array( 167 | 'value' => 'bettman', 168 | 'group' => 'counts' 169 | ) 170 | ); 171 | 172 | $groups = array(); 173 | foreach ( $values as $key => $value ) { 174 | if ( isset( $value['group'] ) ) 175 | $groups[] = $value['group']; 176 | else 177 | $groups[] = 'default'; 178 | } 179 | 180 | $keyed_values = array(); 181 | foreach ( $values as $key => $value ) { 182 | if ( isset( $value['group'] ) ) 183 | $keyed_values[ $this->object_cache->buildKey( $key, $value['group'] ) ] = $value['value']; 184 | else 185 | $keyed_values[ $this->object_cache->buildKey( $key, 'default' ) ] = $value['value']; 186 | } 187 | 188 | // Add each key individually 189 | foreach ( $values as $key => $value ) { 190 | if ( isset( $value['group'] ) ) 191 | $this->assertTrue( $this->object_cache->set( $key, $value['value'], $value['group'] ) ); 192 | else 193 | $this->assertTrue( $this->object_cache->set( $key, $value['value'] ) ); 194 | } 195 | 196 | // Remove a value from internal cache 197 | $this->assertSame( 'gretzky', $this->object_cache->cache[ $this->object_cache->buildKey( 99, 'oilers' ) ] ); 198 | unset( $this->object_cache->cache[ $this->object_cache->buildKey( 99, 'oilers' ) ] ); 199 | $this->assertFalse( isset( $this->object_cache->cache[ $this->object_cache->buildKey( 99, 'oilers' ) ] ) ); 200 | 201 | // Test that return is same as input 202 | $this->assertEmpty( array_diff( $keyed_values, $this->object_cache->getMulti( array_keys( $values ), $groups ) ) ); 203 | 204 | // Test order when sending the 4th arg 205 | $this->assertSame( $keyed_values, $this->object_cache->getMulti( array_keys( $values ), $groups, '', $cas_tokens, Memcached::GET_PRESERVE_ORDER ) ); 206 | 207 | // Make sure that the no_mc_group value never made it to memcached 208 | $this->assertFalse( $this->object_cache->m->get( $this->object_cache->buildKey( 'the-one-with-no-name', 'counts' ) ) ); 209 | 210 | // Verify the no_mc_group value is in the internal cache 211 | $this->assertSame( 'bettman', $this->object_cache->cache[ $this->object_cache->buildKey( 'the-one-with-no-name', 'counts' ) ] ); 212 | } 213 | 214 | public function test_get_multi_cas_tokens() { 215 | $values = array( 216 | 87 => 'crosby', 217 | 99 => 'gretzky', 218 | '68' => 'jagr' 219 | ); 220 | 221 | $keyed_values = array(); 222 | foreach ( $values as $key => $value ) 223 | $keyed_values[ $this->object_cache->buildKey( $key, 'default' ) ] = $value; 224 | 225 | // Add each key individually 226 | foreach ( $values as $key => $value ) 227 | $this->assertTrue( $this->object_cache->set( $key, $value ) ); 228 | 229 | // Test that return is same as input 230 | $this->assertEmpty( array_diff( $keyed_values, $this->object_cache->getMulti( array_keys( $values ) ) ) ); 231 | 232 | // Test that return is same as input with same order 233 | $this->assertSame( $keyed_values, $this->object_cache->getMulti( array_keys( $values ), 'default', '', $cas_tokens, Memcached::GET_PRESERVE_ORDER ) ); 234 | 235 | // Verify CAS tokens 236 | $this->assertTrue( isset( $cas_tokens ) ); 237 | 238 | foreach ( $cas_tokens as $token ) 239 | $this->assertTrue( is_float( $token ) && $token > 0 ); 240 | } 241 | 242 | public function test_get_multi_by_key_gets_multiple_values_if_added_individually() { 243 | $values = array( 244 | 87 => 'crosby', 245 | 99 => 'gretzky', 246 | '68' => 'jagr' 247 | ); 248 | 249 | $server_key = 'stark'; 250 | 251 | $keyed_values = array(); 252 | foreach ( $values as $key => $value ) 253 | $keyed_values[ $this->object_cache->buildKey( $key, 'default' ) ] = $value; 254 | 255 | // Add each key individually 256 | foreach ( $values as $key => $value ) 257 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value ) ); 258 | 259 | // Test that return is same as input 260 | $this->assertEmpty( array_diff( $keyed_values, $this->object_cache->getMultiByKey( $server_key, array_keys( $values ) ) ) ); 261 | 262 | // Test that return is same as input with same order 263 | $this->assertSame( $keyed_values, $this->object_cache->getMultiByKey( $server_key, array_keys( $values ), 'default', $cas_tokens, Memcached::GET_PRESERVE_ORDER ) ); 264 | } 265 | 266 | public function test_get_multi_by_key_gets_multiple_values_if_added_individually_with_different_groups() { 267 | $values = array( 268 | 87 => array( 269 | 'value' => 'crosby', 270 | 'group' => 'penguins' 271 | ), 272 | 99 => array( 273 | 'value' => 'gretzky', 274 | 'group' => 'oilers' 275 | ), 276 | '68' => array( 277 | 'value' => 'jagr' 278 | ) 279 | ); 280 | 281 | $server_key = 'test-key'; 282 | 283 | $groups = array(); 284 | foreach ( $values as $key => $value ) { 285 | if ( isset( $value['group'] ) ) 286 | $groups[] = $value['group']; 287 | } 288 | 289 | $keyed_values = array(); 290 | foreach ( $values as $key => $value ) { 291 | if ( isset( $value['group'] ) ) 292 | $keyed_values[ $this->object_cache->buildKey( $key, $value['group'] ) ] = $value['value']; 293 | else 294 | $keyed_values[ $this->object_cache->buildKey( $key, 'default' ) ] = $value['value']; 295 | } 296 | 297 | // Add each key individually 298 | foreach ( $values as $key => $value ) { 299 | if ( isset( $value['group'] ) ) 300 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value['value'], $value['group'] ) ); 301 | else 302 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value['value'] ) ); 303 | } 304 | 305 | // Test that return is same as input 306 | $this->assertEmpty( array_diff( $keyed_values, $this->object_cache->getMultiByKey( $server_key, array_keys( $values ), $groups ) ) ); 307 | 308 | // Test for same order with flag set 309 | $this->assertSame( $keyed_values, $this->object_cache->getMultiByKey( $server_key, array_keys( $values ), $groups, $cas_tokens, Memcached::GET_PRESERVE_ORDER ) ); 310 | } 311 | 312 | public function test_get_multi_by_key_gets_multiple_values_if_added_individually_with_different_groups_including_no_mc_groups() { 313 | $values = array( 314 | 87 => array( 315 | 'value' => 'crosby', 316 | 'group' => 'penguins' 317 | ), 318 | 99 => array( 319 | 'value' => 'gretzky', 320 | 'group' => 'oilers' 321 | ), 322 | '68' => array( 323 | 'value' => 'jagr' 324 | ), 325 | // If these values are changed, be sure to change hardcoded versions below 326 | 'the-one-with-no-name' => array( 327 | 'value' => 'bettman', 328 | 'group' => 'counts' 329 | ) 330 | ); 331 | 332 | $server_key = 'a-really-nice-key'; 333 | 334 | $groups = array(); 335 | foreach ( $values as $key => $value ) { 336 | if ( isset( $value['group'] ) ) 337 | $groups[] = $value['group']; 338 | else 339 | $groups[] = 'default'; 340 | } 341 | 342 | $keyed_values = array(); 343 | foreach ( $values as $key => $value ) { 344 | if ( isset( $value['group'] ) ) 345 | $keyed_values[ $this->object_cache->buildKey( $key, $value['group'] ) ] = $value['value']; 346 | else 347 | $keyed_values[ $this->object_cache->buildKey( $key, 'default' ) ] = $value['value']; 348 | } 349 | 350 | // Add each key individually 351 | foreach ( $values as $key => $value ) { 352 | if ( isset( $value['group'] ) ) 353 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value['value'], $value['group'] ) ); 354 | else 355 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value['value'] ) ); 356 | } 357 | 358 | // Test that return is same as input 359 | $this->assertSame( $keyed_values, $this->object_cache->getMultiByKey( $server_key, array_keys( $values ), $groups ) ); 360 | 361 | // Make sure that the no_mc_group value never made it to memcached 362 | $this->assertFalse( $this->object_cache->m->get( $this->object_cache->buildKey( 'the-one-with-no-name', 'counts' ) ) ); 363 | 364 | // Verify the no_mc_group value is in the internal cache 365 | $this->assertSame( 'bettman', $this->object_cache->cache[ $this->object_cache->buildKey( 'the-one-with-no-name', 'counts' ) ] ); 366 | } 367 | 368 | public function test_get_multi_by_key_gets_multiple_values_if_some_are_not_in_internal_cache() { 369 | $values = array( 370 | 87 => 'crosby', 371 | 99 => 'gretzky', 372 | '68' => 'jagr' 373 | ); 374 | 375 | $server_key = 'a-delight'; 376 | 377 | $keyed_values = array(); 378 | foreach ( $values as $key => $value ) 379 | $keyed_values[ $this->object_cache->buildKey( $key, 'default' ) ] = $value; 380 | 381 | // Add each key individually 382 | foreach ( $values as $key => $value ) 383 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value ) ); 384 | 385 | // Remove a value from internal cache 386 | $this->assertSame( 'gretzky', $this->object_cache->cache[ $this->object_cache->buildKey( 99 ) ] ); 387 | unset( $this->object_cache->cache[ $this->object_cache->buildKey( 99 ) ] ); 388 | $this->assertFalse( isset( $this->object_cache->cache[ $this->object_cache->buildKey( 99 ) ] ) ); 389 | 390 | // Test that the right values are returned is the wrong order without the flag set 391 | $this->assertEmpty( array_diff( $keyed_values, $this->object_cache->getMultiByKey( $server_key, array_keys( $values ) ) ) ); 392 | 393 | // Test that return is same as input 394 | $this->assertSame( $keyed_values, $this->object_cache->getMultiByKey( $server_key, array_keys( $values ), 'default', $cas_tokens, Memcached::GET_PRESERVE_ORDER ) ); 395 | } 396 | 397 | public function test_get_multi_by_key_order_if_added_individually_with_different_groups_including_no_mc_groups_and_if_some_are_not_in_internal_cache() { 398 | $values = array( 399 | 87 => array( 400 | 'value' => 'crosby', 401 | 'group' => 'penguins' 402 | ), 403 | 99 => array( 404 | 'value' => 'gretzky', 405 | 'group' => 'oilers' 406 | ), 407 | '68' => array( 408 | 'value' => 'jagr' 409 | ), 410 | // If these values are changed, be sure to change hardcoded versions below 411 | 'the-one-with-no-name' => array( 412 | 'value' => 'bettman', 413 | 'group' => 'counts' 414 | ) 415 | ); 416 | 417 | $server_key = 'holly-jolly'; 418 | 419 | $groups = array(); 420 | foreach ( $values as $key => $value ) { 421 | if ( isset( $value['group'] ) ) 422 | $groups[] = $value['group']; 423 | else 424 | $groups[] = 'default'; 425 | } 426 | 427 | $keyed_values = array(); 428 | foreach ( $values as $key => $value ) { 429 | if ( isset( $value['group'] ) ) 430 | $keyed_values[ $this->object_cache->buildKey( $key, $value['group'] ) ] = $value['value']; 431 | else 432 | $keyed_values[ $this->object_cache->buildKey( $key, 'default' ) ] = $value['value']; 433 | } 434 | 435 | // Add each key individually 436 | foreach ( $values as $key => $value ) { 437 | if ( isset( $value['group'] ) ) 438 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value['value'], $value['group'] ) ); 439 | else 440 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value['value'] ) ); 441 | } 442 | 443 | // Remove a value from internal cache 444 | $this->assertSame( 'gretzky', $this->object_cache->cache[ $this->object_cache->buildKey( 99, 'oilers' ) ] ); 445 | unset( $this->object_cache->cache[ $this->object_cache->buildKey( 99, 'oilers' ) ] ); 446 | $this->assertFalse( isset( $this->object_cache->cache[ $this->object_cache->buildKey( 99, 'oilers' ) ] ) ); 447 | 448 | // Test that return is same as input 449 | $this->assertEmpty( array_diff( $keyed_values, $this->object_cache->getMultiByKey( $server_key, array_keys( $values ), $groups ) ) ); 450 | 451 | // Test order when sending the 4th arg 452 | $this->assertSame( $keyed_values, $this->object_cache->getMultiByKey( $server_key, array_keys( $values ), $groups, $cas_tokens, Memcached::GET_PRESERVE_ORDER ) ); 453 | 454 | // Make sure that the no_mc_group value never made it to memcached 455 | $this->assertFalse( $this->object_cache->m->get( $this->object_cache->buildKey( 'the-one-with-no-name', 'counts' ) ) ); 456 | 457 | // Verify the no_mc_group value is in the internal cache 458 | $this->assertSame( 'bettman', $this->object_cache->cache[ $this->object_cache->buildKey( 'the-one-with-no-name', 'counts' ) ] ); 459 | } 460 | 461 | public function test_get_multi_by_key_cas_tokens() { 462 | $values = array( 463 | 87 => 'crosby', 464 | 99 => 'gretzky', 465 | '68' => 'jagr' 466 | ); 467 | 468 | $server_key = 'songo'; 469 | 470 | $keyed_values = array(); 471 | foreach ( $values as $key => $value ) 472 | $keyed_values[ $this->object_cache->buildKey( $key, 'default' ) ] = $value; 473 | 474 | // Add each key individually 475 | foreach ( $values as $key => $value ) 476 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value ) ); 477 | 478 | // Test that return is same as input 479 | $this->assertEmpty( array_diff( $keyed_values, $this->object_cache->getMultiByKey( $server_key, array_keys( $values ) ) ) ); 480 | 481 | // Test that return is same as input with same order 482 | $this->assertSame( $keyed_values, $this->object_cache->getMultiByKey( $server_key, array_keys( $values ), 'default', $cas_tokens, Memcached::GET_PRESERVE_ORDER ) ); 483 | 484 | // Verify CAS tokens 485 | $this->assertTrue( isset( $cas_tokens ) ); 486 | 487 | foreach ( $cas_tokens as $token ) 488 | $this->assertTrue( is_float( $token ) && $token > 0 ); 489 | } 490 | } -------------------------------------------------------------------------------- /tests/tests/get.php: -------------------------------------------------------------------------------- 1 | assertTrue( $this->object_cache->add( $key, $value ) ); 10 | 11 | // Verify correct value is returned 12 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 13 | } 14 | 15 | public function test_get_value_twice() { 16 | $key = microtime(); 17 | $value = 'brodeur'; 18 | 19 | // Add string to memcached 20 | $this->assertTrue( $this->object_cache->add( $key, $value ) ); 21 | 22 | // Verify correct value is returned 23 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 24 | 25 | // Verify correct value is returned when pulled from the internal cache 26 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 27 | } 28 | 29 | public function test_get_value_with_group() { 30 | $key = microtime(); 31 | $value = 'brodeur'; 32 | 33 | $group = 'devils'; 34 | 35 | // Add string to memcached 36 | $this->assertTrue( $this->object_cache->add( $key, $value, $group ) ); 37 | 38 | // Verify correct value is returned 39 | $this->assertSame( $value, $this->object_cache->get( $key, $group ) ); 40 | } 41 | 42 | public function test_get_value_with_no_mc_group() { 43 | $key = microtime(); 44 | $value = 'brodeur'; 45 | 46 | $group = 'comment'; 47 | 48 | // Add string to memcached 49 | $this->assertTrue( $this->object_cache->add( $key, $value, $group ) ); 50 | 51 | // Verify correct value is returned 52 | $this->assertSame( $value, $this->object_cache->get( $key, $group ) ); 53 | } 54 | 55 | public function test_get_value_with_global_group() { 56 | $key = microtime(); 57 | $value = 'brodeur'; 58 | 59 | $group = 'usermeta'; 60 | 61 | // Add string to memcached 62 | $this->assertTrue( $this->object_cache->add( $key, $value, $group ) ); 63 | 64 | // Verify correct value is returned 65 | $this->assertSame( $value, $this->object_cache->get( $key, $group ) ); 66 | } 67 | 68 | public function test_get_value_with_found_indicator() { 69 | $key = microtime(); 70 | $value = 'karlson'; 71 | $group = 'senators'; 72 | $found = false; 73 | 74 | // Add string to memcached 75 | $this->assertTrue( $this->object_cache->add( $key, $value, $group ) ); 76 | 77 | // Verify correct value is returned 78 | $this->assertSame( $value, $this->object_cache->get( $key, $group, false, $found ) ); 79 | 80 | // Verify that found variable is set to true because the item was found 81 | $this->assertTrue( $found ); 82 | } 83 | 84 | public function test_get_value_with_found_indicator_when_value_is_not_found() { 85 | $key = microtime(); 86 | $value = 'neil'; 87 | $group = 'senators'; 88 | $found = false; 89 | 90 | // Add string to memcached 91 | $this->assertTrue( $this->object_cache->add( $key, $value, $group ) ); 92 | 93 | // Verify that the value is deleted 94 | $this->assertTrue( $this->object_cache->delete( $key, $group ) ); 95 | 96 | // Verify that false is returned 97 | $this->assertFalse( $this->object_cache->get( $key, $group, false, $found ) ); 98 | 99 | // Verify that found variable is set to true because the item was found 100 | $this->assertFalse( $found ); 101 | } 102 | 103 | public function test_get_value_with_found_indicator_when_retrieved_from_memcached() { 104 | $key = microtime(); 105 | $value = 'holtby'; 106 | $group = 'capitals'; 107 | $found = false; 108 | 109 | // Add string to memcached 110 | $this->assertTrue( $this->object_cache->add( $key, $value, $group ) ); 111 | 112 | // Remove from internal cache and verify 113 | unset( $this->object_cache->cache[ $this->object_cache->buildKey( $key, $group ) ] ); 114 | $this->assertFalse( $this->object_cache->get_from_runtime_cache( $key, $group ) ); 115 | 116 | // Verify correct value is returned 117 | $this->assertSame( $value, $this->object_cache->get( $key, $group, false, $found ) ); 118 | 119 | // Verify that found variable is set to true because the item was found 120 | $this->assertTrue( $found ); 121 | } 122 | 123 | public function test_get_value_with_found_indicator_when_retrieved_from_memcached_and_value_is_not_found() { 124 | $key = microtime(); 125 | $value = 'backstrom'; 126 | $group = 'capitals'; 127 | $found = false; 128 | 129 | // Add string to memcached 130 | $this->assertTrue( $this->object_cache->add( $key, $value, $group ) ); 131 | 132 | // Remove from internal cache and verify 133 | unset( $this->object_cache->cache[ $this->object_cache->buildKey( $key, $group ) ] ); 134 | $this->assertFalse( $this->object_cache->get_from_runtime_cache( $key, $group ) ); 135 | 136 | // Verify that the value is deleted 137 | $this->assertTrue( $this->object_cache->delete( $key, $group ) ); 138 | 139 | // Verify that false is returned 140 | $this->assertFalse( $this->object_cache->get( $key, $group, false, $found ) ); 141 | 142 | // Verify that found variable is set to true because the item was found 143 | $this->assertFalse( $found ); 144 | } 145 | 146 | public function test_get_value_with_callback_with_true_response() { 147 | $key = microtime(); 148 | $group = 'nj-devils'; 149 | 150 | $value = 'brodeur'; 151 | 152 | // Verify that callback sets value correctly 153 | $this->assertSame( $value, $this->object_cache->get( $key, $group, false, $found, '', true, 'memcached_get_callback_true' ) ); 154 | 155 | // Doublecheck it 156 | $this->assertSame( $value, $this->object_cache->get( $key, $group ) ); 157 | } 158 | 159 | public function test_get_value_with_callback_with_false_response() { 160 | $key = microtime(); 161 | $group = 'nhl-nj-devils'; 162 | 163 | $value = 'brodeur'; 164 | 165 | // Verify that callback sets value correctly 166 | $this->assertFalse( $this->object_cache->get( $key, $group, false, $found, '', false, 'memcached_get_callback_false' ) ); 167 | 168 | // Doublecheck it 169 | $this->assertFalse( $this->object_cache->get( $key, $group ) ); 170 | } 171 | 172 | public function test_get_value_with_callback_with_true_response_and_using_class_method() { 173 | $key = microtime(); 174 | $group = 'nhl-nj-devils-team'; 175 | 176 | $value = 'brodeur'; 177 | 178 | // Verify that callback sets value correctly 179 | $this->assertSame( $value, $this->object_cache->get( $key, $group, false, $found, '', false, array( &$this, 'memcached_get_callback_true_class_method' ) ) ); 180 | 181 | // Doublecheck it 182 | $this->assertSame( $value, $this->object_cache->get( $key, $group ) ); 183 | } 184 | 185 | public function memcached_get_callback_true_class_method( $m, $key, &$value ) { 186 | $value = 'brodeur'; 187 | return true; 188 | } 189 | 190 | public function test_get_value_with_callback_with_false_response_and_using_class_method() { 191 | $key = microtime(); 192 | $group = 'nhl-nj-devils-team-runner-up'; 193 | 194 | // Verify that callback sets value correctly 195 | $this->assertFalse( $this->object_cache->get( $key, $group, false, $found, '', false, array( &$this, 'memcached_get_callback_false_class_method' ) ) ); 196 | 197 | // Doublecheck it 198 | $this->assertFalse( $this->object_cache->get( $key, $group ) ); 199 | } 200 | 201 | public function memcached_get_callback_false_class_method( $m, $key, &$value ) { 202 | $value = 'brodeur'; 203 | return false; 204 | } 205 | 206 | public function test_get_value_with_callback_ignores_callback_for_no_mc_group() { 207 | $key = microtime(); 208 | $group = 'comment'; 209 | 210 | $value = 'brodeur'; 211 | 212 | // Verify that if completely bypassed 213 | $this->assertFalse( $this->object_cache->get( $key, $group, false, $found, '', false, array( &$this, 'memcached_get_callback_true_no_mc_group' ) ) ); 214 | 215 | // Doublecheck that no value has been set 216 | $this->assertFalse( $this->object_cache->get( $key, $group ) ); 217 | 218 | // Verify that a normal set and get works when a callback is sent 219 | $this->assertTrue( $this->object_cache->set( $key, $value, $group ) ); 220 | $this->assertSame( $value, $this->object_cache->get( $key, $group, false, $found, '', false, array( &$this, 'memcached_get_callback_true_no_mc_group' ) ) ); 221 | } 222 | 223 | public function memcached_get_callback_true_no_mc_group( $m, $key, &$value ) { 224 | $value = 'parise'; 225 | return true; 226 | } 227 | 228 | public function test_get_value_and_return_cas_token() { 229 | $key = microtime(); 230 | 231 | $value = 'ovechkin'; 232 | $new_value = 'crosby'; 233 | 234 | // Add value 235 | $this->assertTrue( $this->object_cache->add( $key, $value ) ); 236 | 237 | // Get CAS token 238 | $this->assertSame( $value, $this->object_cache->get( $key, 'default', false, $found, '', false, null, $cas_token ) ); 239 | 240 | // Verify that we have a CAS token 241 | $this->assertTrue( is_float( $cas_token ) ); 242 | 243 | // Add value via cas 244 | $this->assertTrue( $this->object_cache->cas( $cas_token, $key, $new_value ) ); 245 | 246 | // Verify the value is correct 247 | $this->assertSame( $new_value, $this->object_cache->get( $key ) ); 248 | } 249 | 250 | public function test_get_value_return_null_cas_token_with_not_found_key() { 251 | $key = microtime(); 252 | 253 | // Return false with value not yet set 254 | $this->assertFalse( $this->object_cache->get( $key, 'default', false, $found, '', false, null, $cas_token ) ); 255 | 256 | // Verify that we have a CAS token 257 | $this->assertTrue( is_null( $cas_token ) ); 258 | } 259 | 260 | public function test_get_value_with_cas_token_and_callback() { 261 | $key = microtime(); 262 | 263 | $value = 'brodeur'; 264 | $group = 'devils'; 265 | 266 | // Set value via the callback when key is not set 267 | $this->assertSame( $value, $this->object_cache->get( $key, $group, false, $found, '', false, array( &$this, 'memcached_get_callback_for_cas_token_and_callback' ), $cas_token ) ); 268 | 269 | // Double check the value 270 | $this->assertSame( $value, $this->object_cache->get( $key, $group ) ); 271 | 272 | // Since not received from memcached, cas_token should be (float) 0 273 | $this->assertTrue( (float) 0 === $cas_token ); 274 | 275 | // The value should now be set and this function should return the same result 276 | $this->assertSame( $value, $this->object_cache->get( $key, $group, false, $found, '', false, array( &$this, 'memcached_get_callback_for_cas_token_and_callback' ), $cas_token ) ); 277 | 278 | // See if we got an acceptable CAS token 279 | $this->assertTrue( is_float( $cas_token ) && $cas_token > 0 ); 280 | } 281 | 282 | public function memcached_get_callback_for_cas_token_and_callback( $m, $key, &$value ) { 283 | $value = 'brodeur'; 284 | return true; 285 | } 286 | 287 | /** 288 | * @expectedException PHPUnit_Framework_Error 289 | */ 290 | public function test_get_expect_exception_when_cache_cb_is_not_callable() { 291 | $key = microtime(); 292 | 293 | $value = 'brodeur'; 294 | $group = 'devils'; 295 | 296 | // Set value via the callback when key is not set 297 | $this->assertSame( $value, $this->object_cache->get( $key, $group, false, $found, '', false, array( &$this, 'fake_function' ) ) ); 298 | 299 | } 300 | 301 | public function test_get_by_key_value() { 302 | $key = microtime(); 303 | $value = 'brodeur'; 304 | 305 | $server_key = microtime(); 306 | 307 | // Add string to memcached 308 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value ) ); 309 | 310 | // Verify correct value is returned 311 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 312 | } 313 | 314 | public function test_get_by_key_value_twice() { 315 | $key = microtime(); 316 | $value = 'brodeur'; 317 | 318 | $server_key = microtime(); 319 | 320 | // Add string to memcached 321 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value ) ); 322 | 323 | // Verify correct value is returned 324 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 325 | 326 | // Verify correct value is returned when pulled from the internal cache 327 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 328 | } 329 | 330 | public function test_get_by_key_value_with_group() { 331 | $key = microtime(); 332 | $value = 'brodeur'; 333 | 334 | $group = 'devils'; 335 | 336 | $server_key = microtime(); 337 | 338 | // Add string to memcached 339 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value, $group ) ); 340 | 341 | // Verify correct value is returned 342 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, $group ) ); 343 | } 344 | 345 | public function test_get_by_key_value_with_no_mc_group() { 346 | $key = microtime(); 347 | $value = 'brodeur'; 348 | 349 | $group = 'comment'; 350 | 351 | $server_key = microtime(); 352 | 353 | // Add string to memcached 354 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value, $group ) ); 355 | 356 | // Verify correct value is returned 357 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, $group ) ); 358 | } 359 | 360 | public function test_get_by_key_value_with_global_group() { 361 | $key = microtime(); 362 | $value = 'brodeur'; 363 | 364 | $group = 'usermeta'; 365 | 366 | $server_key = microtime(); 367 | 368 | // Add string to memcached 369 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value, $group ) ); 370 | 371 | // Verify correct value is returned 372 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, $group ) ); 373 | } 374 | 375 | public function test_get_by_key_value_with_found_indicator() { 376 | $key = microtime(); 377 | $server_key = microtime(); 378 | $value = 'johansen'; 379 | $group = 'senators'; 380 | $found = false; 381 | 382 | // Add string to memcached 383 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value, $group ) ); 384 | 385 | // Verify correct value is returned 386 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, $group, false, $found ) ); 387 | 388 | // Verify that found variable is set to true because the item was found 389 | $this->assertTrue( $found ); 390 | } 391 | 392 | public function test_get_by_key_value_with_found_indicator_when_value_is_not_found() { 393 | $key = microtime(); 394 | $server_key = microtime(); 395 | $value = 'fisher'; 396 | $group = 'senators'; 397 | $found = false; 398 | 399 | // Add string to memcached 400 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value, $group ) ); 401 | 402 | // Verify that the value is deleted 403 | $this->assertTrue( $this->object_cache->deleteByKey( $server_key, $key, $group ) ); 404 | 405 | // Verify that false is returned 406 | $this->assertFalse( $this->object_cache->getByKey( $server_key, $key, $group, false, $found ) ); 407 | 408 | // Verify that found variable is set to true because the item was found 409 | $this->assertFalse( $found ); 410 | } 411 | 412 | public function test_get_by_key_value_with_found_indicator_when_retrieved_from_memcached() { 413 | $key = microtime(); 414 | $server_key = microtime(); 415 | $value = 'ovechkin'; 416 | $group = 'capitals'; 417 | $found = false; 418 | 419 | // Add string to memcached 420 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value, $group ) ); 421 | 422 | // Remove from internal cache and verify 423 | unset( $this->object_cache->cache[ $this->object_cache->buildKey( $key, $group ) ] ); 424 | $this->assertFalse( $this->object_cache->get_from_runtime_cache( $key, $group ) ); 425 | 426 | // Verify correct value is returned 427 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, $group, false, $found ) ); 428 | 429 | // Verify that found variable is set to true because the item was found 430 | $this->assertTrue( $found ); 431 | } 432 | 433 | public function test_get_by_key_value_with_found_indicator_when_retrieved_from_memcached_and_value_is_not_found() { 434 | $key = microtime(); 435 | $server_key = microtime(); 436 | $value = 'simmonds'; 437 | $group = 'flyers'; 438 | $found = false; 439 | 440 | // Add string to memcached 441 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value, $group ) ); 442 | 443 | // Remove from internal cache and verify 444 | unset( $this->object_cache->cache[ $this->object_cache->buildKey( $key, $group ) ] ); 445 | $this->assertFalse( $this->object_cache->get_from_runtime_cache( $key, $group ) ); 446 | 447 | // Verify that the value is deleted 448 | $this->assertTrue( $this->object_cache->deleteByKey( $server_key, $key, $group ) ); 449 | 450 | // Verify that false is returned 451 | $this->assertFalse( $this->object_cache->getByKey( $server_key, $key, $group, false, $found ) ); 452 | 453 | // Verify that found variable is set to true because the item was found 454 | $this->assertFalse( $found ); 455 | } 456 | 457 | public function test_get_by_key_value_with_callback_with_true_response() { 458 | $key = microtime(); 459 | $group = 'nj-devils'; 460 | 461 | $value = 'brodeur'; 462 | 463 | $server_key = microtime(); 464 | 465 | // Verify that callback sets value correctly 466 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, $group, false, $found, 'memcached_get_callback_true' ) ); 467 | 468 | // Doublecheck it 469 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, $group ) ); 470 | } 471 | 472 | public function test_get_by_key_value_with_callback_with_false_response() { 473 | $key = microtime(); 474 | $group = 'nhl-nj-devils'; 475 | 476 | $server_key = microtime(); 477 | 478 | // Verify that callback sets value correctly 479 | $this->assertFalse( $this->object_cache->getByKey( $server_key, $key, $group, false, $found, 'memcached_get_callback_false' ) ); 480 | 481 | // Doublecheck it 482 | $this->assertFalse( $this->object_cache->getByKey( $server_key, $key, $group ) ); 483 | } 484 | 485 | public function test_get_by_key_value_with_callback_with_true_response_and_using_class_method() { 486 | $key = microtime(); 487 | $group = 'nhl-nj-devils-team'; 488 | 489 | $value = 'brodeur'; 490 | 491 | $server_key = microtime(); 492 | 493 | // Verify that callback sets value correctly 494 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, $group, false, $found, array( &$this, 'memcached_get_callback_true_class_method' ) ) ); 495 | 496 | // Doublecheck it 497 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, $group ) ); 498 | } 499 | 500 | public function test_get_by_key_value_with_callback_with_false_response_and_using_class_method() { 501 | $key = microtime(); 502 | $group = 'nhl-nj-devils-team-runner-up'; 503 | 504 | $server_key = microtime(); 505 | 506 | // Verify that callback sets value correctly 507 | $this->assertFalse( $this->object_cache->getByKey( $server_key, $key, $group, false, $found, array( &$this, 'memcached_get_callback_false_class_method' ) ) ); 508 | 509 | // Doublecheck it 510 | $this->assertFalse( $this->object_cache->getByKey( $server_key, $key, $group ) ); 511 | } 512 | 513 | public function test_get_by_key_value_with_callback_ignores_callback_for_no_mc_group() { 514 | $key = microtime(); 515 | $group = 'comment'; 516 | 517 | $value = 'brodeur'; 518 | 519 | $server_key = microtime(); 520 | 521 | // Verify that if completely bypassed 522 | $this->assertFalse( $this->object_cache->getByKey( $server_key, $key, $group, false, $found, array( &$this, 'memcached_get_callback_true_no_mc_group' ) ) ); 523 | 524 | // Doublecheck that no value has been set 525 | $this->assertFalse( $this->object_cache->getByKey( $server_key, $key, $group ) ); 526 | 527 | // Verify that a normal set and get works when a callback is sent 528 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value, $group ) ); 529 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, $group, false, $found, array( &$this, 'memcached_get_callback_true_no_mc_group' ) ) ); 530 | } 531 | 532 | public function test_get_by_value_value_and_return_cas_token() { 533 | $key = microtime(); 534 | 535 | $value = 'ovechkin'; 536 | $new_value = 'crosby'; 537 | 538 | $server_key = microtime(); 539 | 540 | // Add value 541 | $this->assertTrue( $this->object_cache->addByKey( $server_key, $key, $value ) ); 542 | 543 | // Get CAS token 544 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, 'default', false, $found, null, $cas_token ) ); 545 | 546 | // Verify that we have a CAS token 547 | $this->assertTrue( is_float( $cas_token ) ); 548 | 549 | // Add value via cas 550 | $this->assertTrue( $this->object_cache->casByKey( $cas_token, $server_key, $key, $new_value ) ); 551 | 552 | // Verify the value is correct 553 | $this->assertSame( $new_value, $this->object_cache->getByKey( $server_key, $key ) ); 554 | } 555 | 556 | public function test_get_by_key_value_return_null_cas_token_with_not_found_key() { 557 | $key = microtime(); 558 | 559 | $server_key = microtime(); 560 | 561 | // Return false with value not yet set 562 | $this->assertFalse( $this->object_cache->getByKey( $server_key, $key, 'default', false, $found, null, $cas_token ) ); 563 | 564 | // Verify that we have a CAS token 565 | $this->assertTrue( is_null( $cas_token ) ); 566 | } 567 | 568 | public function test_get_by_key_value_with_cas_token_and_callback() { 569 | $key = microtime(); 570 | 571 | $value = 'brodeur'; 572 | $group = 'wild'; 573 | 574 | $server_key = 'devils'; 575 | 576 | // Set value via the callback when key is not set 577 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, $group, false, $found, array( &$this, 'memcached_get_callback_for_cas_token_and_callback' ), $cas_token ) ); 578 | 579 | // Since not received from memcached, cas_token should be (float) 0 580 | $this->assertTrue( (float) 0 === $cas_token ); 581 | 582 | // Double check the value 583 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, $group ) ); 584 | 585 | // The value should now be set and this function should return the same result 586 | $result = $this->object_cache->getByKey( $server_key, $key, $group, false, $found, array( &$this, 'memcached_get_callback_for_cas_token_and_callback' ), $cas_token2 ); 587 | $this->assertSame( $value, $result ); 588 | 589 | // See if we got an acceptable CAS token 590 | $this->assertTrue( is_float( $cas_token2 ) ); 591 | } 592 | 593 | /** 594 | * @expectedException PHPUnit_Framework_Error 595 | */ 596 | public function test_get_by_key_expect_exception_when_cache_cb_is_not_callable() { 597 | $key = microtime(); 598 | 599 | $value = 'brodeur'; 600 | $group = 'devils'; 601 | 602 | $server_key = microtime(); 603 | 604 | // Set value via the callback when key is not set 605 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key, $group, false, $found, array( &$this, 'fake_function' ) ) ); 606 | } 607 | } 608 | 609 | function memcached_get_callback_true( $m, $key, &$value ) { 610 | $value = 'brodeur'; 611 | return true; 612 | } 613 | 614 | function memcached_get_callback_false( $m, $key, &$value ) { 615 | $value = 'brodeur'; 616 | return false; 617 | } -------------------------------------------------------------------------------- /tests/tests/get-delayed.php: -------------------------------------------------------------------------------- 1 | 10, 8 | microtime() . '-toews' => 'nineteen' 9 | ); 10 | 11 | $rekeyed = array(); 12 | foreach ( $keys as $key => $value ) 13 | $rekeyed[$this->object_cache->buildKey( $key )] = $value; 14 | 15 | // Set each value 16 | foreach ( $keys as $key => $value ) { 17 | $this->assertTrue( $this->object_cache->set( $key, $value ) ); 18 | } 19 | 20 | // Verify each value 21 | foreach ( $keys as $key => $value ) { 22 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 23 | } 24 | 25 | // getDelayed to retrieve the objects 26 | $this->assertTrue( $this->object_cache->getDelayed( array_keys( $keys ) ) ); 27 | 28 | // Loop through the initial values and see if they match what was returned 29 | // Loop through the initial values and see if an appropriate value was returned. Note that they likely won't come back in the same order. 30 | foreach ( $keys as $key => $value ) { 31 | $next = $this->object_cache->fetch(); 32 | $this->assertTrue( isset( $rekeyed[$next['key']] ) ); 33 | $this->assertSame( $rekeyed[$next['key']], $next['value'] ); 34 | } 35 | 36 | // Doing another fetch call should result in false 37 | $this->assertFalse( $this->object_cache->fetch() ); 38 | 39 | // Finally, that last operation should result in the result code of Memcached::RES_NOTFOUND 40 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 41 | } 42 | 43 | public function test_get_delayed_returns_correct_values_with_different_groups() { 44 | $keys = array( 45 | microtime() . '-sharp' => array( 46 | 'value' => 10, 47 | 'group' => 'blackhawks' 48 | ), 49 | microtime() . '-toews' => array( 50 | 'value' => 'nineteen', 51 | 'group' => 'blackhawks' 52 | ), 53 | microtime() . '-crosby' => array( 54 | 'value' => '87', 55 | 'group' => 'pengiuns' 56 | ), 57 | microtime() . '-suter' => array( 58 | 'value' => 'twenty', 59 | 'group' => 'default' 60 | ), 61 | microtime() . '-bettman' => array( 62 | 'value' => 'commish' 63 | ) 64 | ); 65 | 66 | $rekeyed = array(); 67 | foreach ( $keys as $key => $value ) { 68 | if ( isset( $value['group'] ) ) 69 | $rekeyed[ $this->object_cache->buildKey( $key, $value['group'] ) ] = $value; 70 | else 71 | $rekeyed[ $this->object_cache->buildKey( $key ) ] = $value; 72 | } 73 | 74 | $groups = array(); 75 | foreach ( $keys as $key => $value ) { 76 | if ( isset( $value['group'] ) ) 77 | $groups[] = $value['group']; 78 | } 79 | 80 | // Set each value 81 | foreach ( $keys as $key => $value ) { 82 | if ( isset( $value['group'] ) ) 83 | $this->assertTrue( $this->object_cache->set( $key, $value['value'], $value['group'] ) ); 84 | else 85 | $this->assertTrue( $this->object_cache->set( $key, $value['value'] ) ); 86 | } 87 | 88 | // Verify each value 89 | foreach ( $keys as $key => $value ) { 90 | if ( isset( $value['group'] ) ) 91 | $this->assertSame( $value['value'], $this->object_cache->get( $key, $value['group'] ) ); 92 | else 93 | $this->assertSame( $value['value'], $this->object_cache->get( $key ) ); 94 | } 95 | 96 | // getDelayed to retrieve the objects 97 | $this->assertTrue( $this->object_cache->getDelayed( array_keys( $keys ), $groups ) ); 98 | 99 | // Loop through the initial values and see if they match what was returned 100 | foreach ( $keys as $key => $value ) { 101 | $next = $this->object_cache->fetch(); 102 | $this->assertTrue( isset( $rekeyed[$next['key']] ) ); 103 | $this->assertSame( $rekeyed[$next['key']]['value'], $next['value'] ); 104 | } 105 | 106 | // Doing another fetch call should result in false 107 | $this->assertFalse( $this->object_cache->fetch() ); 108 | 109 | // Finally, that last operation should result in the result code of Memcached::RES_NOTFOUND 110 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 111 | } 112 | 113 | public function test_get_delayed_returns_correct_values_with_cas_tokens() { 114 | $keys = array( 115 | microtime() . '-sharp' => 10, 116 | microtime() . '-toews' => 'nineteen' 117 | ); 118 | 119 | $rekeyed = array(); 120 | foreach ( $keys as $key => $value ) 121 | $rekeyed[$this->object_cache->buildKey( $key )] = $value; 122 | 123 | // Set each value 124 | foreach ( $keys as $key => $value ) { 125 | $this->assertTrue( $this->object_cache->set( $key, $value ) ); 126 | } 127 | 128 | // Verify each value 129 | foreach ( $keys as $key => $value ) { 130 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 131 | } 132 | 133 | // getDelayed to retrieve the objects 134 | $this->assertTrue( $this->object_cache->getDelayed( array_keys( $keys ), 'default', true ) ); 135 | 136 | // Loop through the initial values and see if they match what was returned 137 | foreach ( $keys as $key => $value ) { 138 | // Get next result 139 | $next = $this->object_cache->fetch(); 140 | 141 | // Test that correct value was returned 142 | $this->assertTrue( isset( $rekeyed[$next['key']] ) ); 143 | $this->assertSame( $rekeyed[$next['key']], $next['value'] ); 144 | 145 | $this->assertTrue( is_float( $next['cas'] ) && $next['cas'] > 0 ); 146 | } 147 | 148 | // Doing another fetch call should result in false 149 | $this->assertFalse( $this->object_cache->fetch() ); 150 | 151 | // Finally, that last operation should result in the result code of Memcached::RES_NOTFOUND 152 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 153 | } 154 | 155 | public function test_get_delayed_returns_correct_values_with_different_groups_and_cas_tokens() { 156 | $keys = array( 157 | microtime() . '-sharp' => array( 158 | 'value' => 10, 159 | 'group' => 'blackhawks' 160 | ), 161 | microtime() . '-toews' => array( 162 | 'value' => 'nineteen', 163 | 'group' => 'blackhawks' 164 | ), 165 | microtime() . '-crosby' => array( 166 | 'value' => '87', 167 | 'group' => 'pengiuns' 168 | ), 169 | microtime() . '-suter' => array( 170 | 'value' => 'twenty', 171 | 'group' => 'default' 172 | ), 173 | microtime() . '-bettman' => array( 174 | 'value' => 'commish' 175 | ) 176 | ); 177 | 178 | $rekeyed = array(); 179 | foreach ( $keys as $key => $value ) { 180 | if ( isset( $value['group'] ) ) 181 | $rekeyed[ $this->object_cache->buildKey( $key, $value['group'] ) ] = $value; 182 | else 183 | $rekeyed[ $this->object_cache->buildKey( $key ) ] = $value; 184 | } 185 | 186 | $groups = array(); 187 | foreach ( $keys as $key => $value ) { 188 | if ( isset( $value['group'] ) ) 189 | $groups[] = $value['group']; 190 | } 191 | 192 | // Set each value 193 | foreach ( $keys as $key => $value ) { 194 | if ( isset( $value['group'] ) ) 195 | $this->assertTrue( $this->object_cache->set( $key, $value['value'], $value['group'] ) ); 196 | else 197 | $this->assertTrue( $this->object_cache->set( $key, $value['value'] ) ); 198 | } 199 | 200 | // Verify each value 201 | foreach ( $keys as $key => $value ) { 202 | if ( isset( $value['group'] ) ) 203 | $this->assertSame( $value['value'], $this->object_cache->get( $key, $value['group'] ) ); 204 | else 205 | $this->assertSame( $value['value'], $this->object_cache->get( $key ) ); 206 | } 207 | 208 | // getDelayed to retrieve the objects 209 | $this->assertTrue( $this->object_cache->getDelayed( array_keys( $keys ), $groups, true ) ); 210 | 211 | // Loop through the initial values and see if they match what was returned 212 | foreach ( $keys as $key => $value ) { 213 | $next = $this->object_cache->fetch(); 214 | $this->assertTrue( isset( $rekeyed[$next['key']] ) ); 215 | $this->assertSame( $rekeyed[$next['key']]['value'], $next['value'] ); 216 | 217 | // Verify appropriate cas token 218 | $this->assertTrue( is_float( $next['cas'] ) && $next['cas'] > 0 ); 219 | } 220 | 221 | // Doing another fetch call should result in false 222 | $this->assertFalse( $this->object_cache->fetch() ); 223 | 224 | // Finally, that last operation should result in the result code of Memcached::RES_NOTFOUND 225 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 226 | } 227 | 228 | public function test_get_delayed_by_key_returns_correct_values_with_fetch() { 229 | $keys = array( 230 | microtime() . '-sharp' => 10, 231 | microtime() . '-toews' => 'nineteen' 232 | ); 233 | 234 | $server_key = microtime(); 235 | 236 | // Set each value 237 | foreach ( $keys as $key => $value ) { 238 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value ) ); 239 | } 240 | 241 | // Verify each value 242 | foreach ( $keys as $key => $value ) { 243 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 244 | } 245 | 246 | // getDelayed to retrieve the objects 247 | $this->assertTrue( $this->object_cache->getDelayedByKey( $server_key, array_keys( $keys ) ) ); 248 | 249 | // Loop through the initial values and see if they match what was returned 250 | foreach ( $keys as $key => $value ) { 251 | $this->assertSame( array( 'key' => $this->object_cache->buildKey( $key, 'default' ), 'value' => $value ), $this->object_cache->fetch() ); 252 | } 253 | 254 | // Doing another fetch call should result in false 255 | $this->assertFalse( $this->object_cache->fetch() ); 256 | 257 | // Finally, that last operation should result in the result code of Memcached::RES_NOTFOUND 258 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 259 | } 260 | 261 | public function test_get_delayed_returns_correct_values_and_calls_callback_function() { 262 | $keys = $this->data_for_get_delayed_with_callbacks(); 263 | 264 | // Set each value 265 | foreach ( $keys as $key => $value ) { 266 | $this->assertTrue( $this->object_cache->set( $key, $value ) ); 267 | } 268 | 269 | // Verify each value 270 | foreach ( $keys as $key => $value ) { 271 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 272 | } 273 | 274 | // getDelayed to retrieve the objects 275 | $this->assertTrue( $this->object_cache->getDelayed( array_keys( $keys ), 'default', false, array( &$this, 'get_delayed_returns_correct_values_and_calls_callback_function' ) ) ); 276 | 277 | // Doing another fetch call should result in false 278 | $this->assertFalse( $this->object_cache->fetch() ); 279 | 280 | // Finally, that last operation should result in the result code of Memcached::RES_NOTFOUND 281 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 282 | } 283 | 284 | public function test_get_delayed_returns_correct_values_and_calls_callback_function_with_cas() { 285 | $keys = $this->data_for_get_delayed_with_callbacks(); 286 | 287 | // Set each value 288 | foreach ( $keys as $key => $value ) { 289 | $this->assertTrue( $this->object_cache->set( $key, $value ) ); 290 | } 291 | 292 | // Verify each value 293 | foreach ( $keys as $key => $value ) { 294 | $this->assertSame( $value, $this->object_cache->get( $key ) ); 295 | } 296 | 297 | // getDelayed to retrieve the objects 298 | $this->assertTrue( $this->object_cache->getDelayed( array_keys( $keys ), 'default', true, array( &$this, 'get_delayed_returns_correct_values_and_calls_callback_function' ) ) ); 299 | 300 | // Doing another fetch call should result in false 301 | $this->assertFalse( $this->object_cache->fetch() ); 302 | 303 | // Finally, that last operation should result in the result code of Memcached::RES_NOTFOUND 304 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 305 | } 306 | 307 | public function get_delayed_returns_correct_values_and_calls_callback_function( $m, $item ) { 308 | $keys = $this->data_for_get_delayed_with_callbacks(); 309 | 310 | $new_keys = array(); 311 | foreach ( $keys as $key => $value ) 312 | $new_keys[ $this->object_cache->buildKey( $key ) ] = $value; 313 | 314 | $this->assertSame( $new_keys[ $item['key'] ], $item['value'] ); 315 | 316 | if ( isset( $item['cas'] ) ) 317 | $this->assertTrue( is_float( $item['cas'] ) && $item['cas'] > 0 ); 318 | 319 | $this->assertTrue( is_a( $m, 'Memcached' ) ); 320 | } 321 | 322 | 323 | public function test_get_delayed_by_key_returns_correct_values() { 324 | $keys = array( 325 | microtime() . '-sharp' => 10, 326 | microtime() . '-toews' => 'nineteen' 327 | ); 328 | 329 | $server_key = 'your-server'; 330 | 331 | // Set each value 332 | foreach ( $keys as $key => $value ) { 333 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value ) ); 334 | } 335 | 336 | // Verify each value 337 | foreach ( $keys as $key => $value ) { 338 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 339 | } 340 | 341 | // getDelayed to retrieve the objects 342 | $this->assertTrue( $this->object_cache->getDelayedByKey( $server_key, array_keys( $keys ) ) ); 343 | 344 | // Loop through the initial values and see if they match what was returned 345 | foreach ( $keys as $key => $value ) { 346 | $this->assertSame( array( 'key' => $this->object_cache->buildKey( $key, 'default' ), 'value' => $value ), $this->object_cache->fetch() ); 347 | } 348 | 349 | // Doing another fetch call should result in false 350 | $this->assertFalse( $this->object_cache->fetch() ); 351 | 352 | // Finally, that last operation should result in the result code of Memcached::RES_NOTFOUND 353 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 354 | } 355 | 356 | public function test_get_delayed_by_key_returns_correct_values_with_different_groups() { 357 | $keys = array( 358 | microtime() . '-sharp' => array( 359 | 'value' => 10, 360 | 'group' => 'blackhawks' 361 | ), 362 | microtime() . '-toews' => array( 363 | 'value' => 'nineteen', 364 | 'group' => 'blackhawks' 365 | ), 366 | microtime() . '-crosby' => array( 367 | 'value' => '87', 368 | 'group' => 'pengiuns' 369 | ), 370 | microtime() . '-suter' => array( 371 | 'value' => 'twenty', 372 | 'group' => 'default' 373 | ), 374 | microtime() . '-bettman' => array( 375 | 'value' => 'commish' 376 | ) 377 | ); 378 | 379 | $groups = array(); 380 | foreach ( $keys as $key => $value ) { 381 | if ( isset( $value['group'] ) ) 382 | $groups[] = $value['group']; 383 | } 384 | 385 | $server_key = 'your-server'; 386 | 387 | // Set each value 388 | foreach ( $keys as $key => $value ) { 389 | if ( isset( $value['group'] ) ) 390 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value['value'], $value['group'] ) ); 391 | else 392 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value['value'] ) ); 393 | } 394 | 395 | // Verify each value 396 | foreach ( $keys as $key => $value ) { 397 | if ( isset( $value['group'] ) ) 398 | $this->assertSame( $value['value'], $this->object_cache->getByKey( $server_key, $key, $value['group'] ) ); 399 | else 400 | $this->assertSame( $value['value'], $this->object_cache->getByKey( $server_key, $key ) ); 401 | } 402 | 403 | // getDelayed to retrieve the objects 404 | $this->assertTrue( $this->object_cache->getDelayedByKey( $server_key, array_keys( $keys ), $groups ) ); 405 | 406 | // Loop through the initial values and see if they match what was returned 407 | foreach ( $keys as $key => $value ) { 408 | if ( isset( $value['group'] ) ) 409 | $this->assertSame( array( 'key' => $this->object_cache->buildKey( $key, $value['group'] ), 'value' => $value['value'] ), $this->object_cache->fetch() ); 410 | else 411 | $this->assertSame( array( 'key' => $this->object_cache->buildKey( $key ), 'value' => $value['value'] ), $this->object_cache->fetch() ); 412 | } 413 | 414 | // Doing another fetch call should result in false 415 | $this->assertFalse( $this->object_cache->fetch() ); 416 | 417 | // Finally, that last operation should result in the result code of Memcached::RES_NOTFOUND 418 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 419 | } 420 | 421 | public function test_get_delayed_by_key_returns_correct_values_with_cas_tokens() { 422 | $keys = array( 423 | microtime() . '-sharp' => 10, 424 | microtime() . '-toews' => 'nineteen' 425 | ); 426 | 427 | $server_key = 'your-server'; 428 | 429 | // Set each value 430 | foreach ( $keys as $key => $value ) { 431 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value ) ); 432 | } 433 | 434 | // Verify each value 435 | foreach ( $keys as $key => $value ) { 436 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 437 | } 438 | 439 | // getDelayed to retrieve the objects 440 | $this->assertTrue( $this->object_cache->getDelayedByKey( $server_key, array_keys( $keys ), 'default', true ) ); 441 | 442 | // Loop through the initial values and see if they match what was returned 443 | foreach ( $keys as $key => $value ) { 444 | // Get next result 445 | $next_result = $this->object_cache->fetch(); 446 | 447 | // Get the cas token and remove from the return array for next assertion 448 | $cas_token = $next_result['cas']; 449 | unset( $next_result['cas'] ); 450 | 451 | // Test that correct value was returned 452 | $this->assertSame( array( 'key' => $this->object_cache->buildKey( $key, 'default' ), 'value' => $value ), $next_result ); 453 | $this->assertTrue( is_float( $cas_token ) && $cas_token > 0 ); 454 | } 455 | 456 | // Doing another fetch call should result in false 457 | $this->assertFalse( $this->object_cache->fetch() ); 458 | 459 | // Finally, that last operation should result in the result code of Memcached::RES_NOTFOUND 460 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 461 | } 462 | 463 | public function test_get_delayed_by_key_returns_correct_values_with_different_groups_and_cas_tokens() { 464 | $keys = array( 465 | microtime() . '-sharp' => array( 466 | 'value' => 10, 467 | 'group' => 'blackhawks' 468 | ), 469 | microtime() . '-toews' => array( 470 | 'value' => 'nineteen', 471 | 'group' => 'blackhawks' 472 | ), 473 | microtime() . '-crosby' => array( 474 | 'value' => '87', 475 | 'group' => 'pengiuns' 476 | ), 477 | microtime() . '-suter' => array( 478 | 'value' => 'twenty', 479 | 'group' => 'default' 480 | ), 481 | microtime() . '-bettman' => array( 482 | 'value' => 'commish' 483 | ) 484 | ); 485 | 486 | $groups = array(); 487 | foreach ( $keys as $key => $value ) { 488 | if ( isset( $value['group'] ) ) 489 | $groups[] = $value['group']; 490 | } 491 | 492 | $server_key = 'your-server'; 493 | 494 | // Set each value 495 | foreach ( $keys as $key => $value ) { 496 | if ( isset( $value['group'] ) ) 497 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value['value'], $value['group'] ) ); 498 | else 499 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value['value'] ) ); 500 | } 501 | 502 | // Verify each value 503 | foreach ( $keys as $key => $value ) { 504 | if ( isset( $value['group'] ) ) 505 | $this->assertSame( $value['value'], $this->object_cache->getByKey( $server_key, $key, $value['group'] ) ); 506 | else 507 | $this->assertSame( $value['value'], $this->object_cache->getByKey( $server_key, $key ) ); 508 | } 509 | 510 | // getDelayed to retrieve the objects 511 | $this->assertTrue( $this->object_cache->getDelayedByKey( $server_key, array_keys( $keys ), $groups, true ) ); 512 | 513 | // Loop through the initial values and see if they match what was returned 514 | foreach ( $keys as $key => $value ) { 515 | // Get next result 516 | $next_result = $this->object_cache->fetch(); 517 | 518 | // Get the cas token and remove from the return array for next assertion 519 | $cas_token = $next_result['cas']; 520 | unset( $next_result['cas'] ); 521 | 522 | if ( isset( $value['group'] ) ) 523 | $this->assertSame( array( 'key' => $this->object_cache->buildKey( $key, $value['group'] ), 'value' => $value['value'] ), $next_result ); 524 | else 525 | $this->assertSame( array( 'key' => $this->object_cache->buildKey( $key ), 'value' => $value['value'] ), $next_result ); 526 | 527 | // Verify appropriate cas token 528 | $this->assertTrue( is_float( $cas_token ) && $cas_token > 0 ); 529 | } 530 | 531 | // Doing another fetch call should result in false 532 | $this->assertFalse( $this->object_cache->fetch() ); 533 | 534 | // Finally, that last operation should result in the result code of Memcached::RES_NOTFOUND 535 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 536 | } 537 | 538 | public function test_get_delayed_by_key_returns_correct_values_and_calls_callback_function() { 539 | $keys = $this->data_for_get_delayed_with_callbacks(); 540 | 541 | $server_key = 'your-server'; 542 | 543 | // Set each value 544 | foreach ( $keys as $key => $value ) { 545 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value ) ); 546 | } 547 | 548 | // Verify each value 549 | foreach ( $keys as $key => $value ) { 550 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 551 | } 552 | 553 | // getDelayed to retrieve the objects 554 | $this->assertTrue( $this->object_cache->getDelayedByKey( $server_key, array_keys( $keys ), 'default', false, array( &$this, 'get_delayed_returns_correct_values_and_calls_callback_function' ) ) ); 555 | 556 | // Doing another fetch call should result in false 557 | $this->assertFalse( $this->object_cache->fetch() ); 558 | 559 | // Finally, that last operation should result in the result code of Memcached::RES_NOTFOUND 560 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 561 | } 562 | 563 | public function test_get_delayed_by_returns_correct_values_and_calls_callback_function_with_cas() { 564 | $keys = $this->data_for_get_delayed_with_callbacks(); 565 | 566 | $server_key = 'your-server'; 567 | 568 | // Set each value 569 | foreach ( $keys as $key => $value ) { 570 | $this->assertTrue( $this->object_cache->setByKey( $server_key, $key, $value ) ); 571 | } 572 | 573 | // Verify each value 574 | foreach ( $keys as $key => $value ) { 575 | $this->assertSame( $value, $this->object_cache->getByKey( $server_key, $key ) ); 576 | } 577 | 578 | // getDelayed to retrieve the objects 579 | $this->assertTrue( $this->object_cache->getDelayedByKey( $server_key, array_keys( $keys ), 'default', true, array( &$this, 'get_delayed_returns_correct_values_and_calls_callback_function' ) ) ); 580 | 581 | // Doing another fetch call should result in false 582 | $this->assertFalse( $this->object_cache->fetch() ); 583 | 584 | // Finally, that last operation should result in the result code of Memcached::RES_NOTFOUND 585 | $this->assertSame( Memcached::RES_NOTFOUND, $this->object_cache->getResultCode() ); 586 | } 587 | 588 | /** 589 | * Used by the main test function and the callback so data is in sync. 590 | * 591 | * @return array 592 | */ 593 | public function data_for_get_delayed_with_callbacks() { 594 | return array( 595 | '-sharp' => 10, 596 | '-toews' => 'nineteen' 597 | ); 598 | } 599 | } --------------------------------------------------------------------------------