Sokil\Mongo\Cache::get PHP Method

get() public method

Get value by key
public get ( $key ) : array | null
$key
return array | null
    public function get($key)
    {
        // Get document
        $document = $this->collection->getDocument($key);
        if (!$document) {
            return null;
        }
        // Mongo deletes document not exactly when set in field
        // Required date checking
        // Expiration may be empty for keys whicj never expired
        $expiredAt = $document->get(self::FIELD_NAME_EXPIRED);
        if ($expiredAt && $expiredAt->sec < time()) {
            return null;
        }
        // Return value
        return $document->get(self::FIELD_NAME_VALUE);
    }

Usage Example

Example #1
0
 public function testDelete()
 {
     $this->cache->set('php', 'PHP: Hypertext Processor', 1);
     $this->assertEquals('PHP: Hypertext Processor', $this->cache->get('php'));
     $this->cache->delete('php');
     $this->assertEmpty($this->cache->get('php'));
 }