Browse Source

Merge pull request #1094 from pmoust/cache-mset

[WIP] Caching: mset() method to store multiple items.
tags/2.0.0-beta
Alexander Makarov 11 years ago
parent
commit
5fc51f1ad9
  1. 17
      framework/yii/caching/Cache.php
  2. 21
      tests/unit/framework/caching/CacheTestCase.php

17
framework/yii/caching/Cache.php

@ -93,7 +93,7 @@ abstract class Cache extends Component implements \ArrayAccess
* If the given key is a string containing alphanumeric characters only and no more than 32 characters, * If the given key is a string containing alphanumeric characters only and no more than 32 characters,
* then the key will be returned back prefixed with [[keyPrefix]]. Otherwise, a normalized key * then the key will be returned back prefixed with [[keyPrefix]]. Otherwise, a normalized key
* is generated by serializing the given key, applying MD5 hashing, and prefixing with [[keyPrefix]]. * is generated by serializing the given key, applying MD5 hashing, and prefixing with [[keyPrefix]].
* *
* @param mixed $key the key to be normalized * @param mixed $key the key to be normalized
* @return string the generated cache key * @return string the generated cache key
*/ */
@ -216,6 +216,21 @@ abstract class Cache extends Component implements \ArrayAccess
} }
/** /**
* Stores multiple items in cache. Each item contains a value identified by a key.
* If the cache already contains such a key, the existing value and
* expiration time will be replaced with the new ones, respectively.
*
* @param array $items the items to be cached, as key-value pairs. Each key can be a simple string or
* a complex data structure consisting of factors representing the key.
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean whether the items are successfully stored into cache
*/
public function mset($items, $expire = 0)
{
return false;
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key. * Stores a value identified by a key into cache if the cache does not contain this key.
* Nothing will be done if the cache already contains the key. * Nothing will be done if the cache already contains the key.
* @param mixed $key a key identifying the value to be cached. This can be a simple string or * @param mixed $key a key identifying the value to be cached. This can be a simple string or

21
tests/unit/framework/caching/CacheTestCase.php

@ -91,6 +91,27 @@ abstract class CacheTestCase extends TestCase
$this->assertEquals('array_test', $array['array_test']); $this->assertEquals('array_test', $array['array_test']);
} }
public function testMset()
{
$this->markTestIncomplete('Work in progress');
$cache = $this->getCacheInstance();
$cache->flush();
$this->assertTrue($cache->mset(['string_test' => 'string_test',
'number_test' => 42,
'array_test' => ['array_test' => 'array_test'],
]));
$this->assertEquals('string_test', $cache->get('string_test'));
$this->assertEquals(42, $cache->get('number_test'));
$array = $cache->get('array_test');
$this->assertArrayHasKey('array_test', $array);
$this->assertEquals('array_test', $array['array_test']);
}
public function testExists() public function testExists()
{ {
$cache = $this->prepare(); $cache = $this->prepare();

Loading…
Cancel
Save