Sokil\Mongo\Cache::setDueDate PHP Method

setDueDate() public method

Set with expiration on concrete date
public setDueDate ( integer | string $key, mixed $value, integer $timestamp, array $tags = null )
$key integer | string
$value mixed
$timestamp integer
$tags array
    public function setDueDate($key, $value, $timestamp, array $tags = null)
    {
        $document = array('_id' => $key, self::FIELD_NAME_VALUE => $value);
        if ($timestamp) {
            $document[self::FIELD_NAME_EXPIRED] = new \MongoDate((int) $timestamp);
        }
        if ($tags) {
            $document[self::FIELD_NAME_TAGS] = $tags;
        }
        $result = $this->collection->getMongoCollection()->update(array('_id' => $key), $document, array('upsert' => true));
        if ((double) 1 !== $result['ok']) {
            throw new Exception('Error setting value');
        }
        return $this;
    }

Usage Example

Example #1
0
 public function testSetDueDate_ExistedKey()
 {
     $this->cache->setDueDate('php', 'Old Value', time() + 100);
     $this->assertEquals('Old Value', $this->cache->get('php'));
     $this->cache->setDueDate('php', 'PHP: Hypertext Processor', time() + 1);
     $this->assertEquals('PHP: Hypertext Processor', $this->cache->get('php'));
     usleep(2000000);
     $this->assertEmpty($this->cache->get('php'));
 }