collection = $collection; } /** * Initialize the DB collection. * Set TTL index. */ protected function initialize(): void { $this->collection->createIndex(['ttl' => 1], ['expireAfterSeconds' => 0]); } /** * Create the default packer for this cache implementation. * * @return PackerInterface */ protected static function createDefaultPacker(): PackerInterface { return new MongoDBBinaryPacker(); } /** * Get filter for key and ttl. * * @param string|iterable $key * @return array */ protected function filter($key) { if (is_array($key)) { $key = ['$in' => $key]; } return [ '_id' => $key, '$or' => [ ['ttl' => ['$gt' => new BSONUTCDateTime($this->currentTimestamp() * 1000)]], ['ttl' => null] ] ]; } /** * {@inheritdoc } */ public function get($key, $default = null) { $filter = $this->filter($this->keyToId($key)); try { $data = $this->collection->findOne($filter); } catch (MongoDBRuntimeException $e) { return $default; } return isset($data) ? $this->unpack($data['value']) : $default; } /** * {@inheritdoc} */ public function getMultiple($keys, $default = null) { $idKeyPairs = $this->mapKeysToIds($keys); if (empty($idKeyPairs)) { return []; } $filter = $this->filter(array_keys($idKeyPairs)); $items = array_fill_keys(array_values($idKeyPairs), $default); try { $rows = $this->collection->find($filter); } catch (MongoDBRuntimeException $e) { return $items; } foreach ($rows as $row) { $id = $row['_id']; $key = $idKeyPairs[$id]; $items[$key] = $this->unpack($row['value']); } return $items; } /** * {@inheritdoc } */ public function has($key) { $filter = $this->filter($this->keyToId($key)); try { $count = $this->collection->count($filter); } catch (MongoDBRuntimeException $e) { return false; } return $count > 0; } /** * {@inheritdoc } */ public function set($key, $value, $ttl = null) { $id = $this->keyToId($key); $item = [ '_id' => $id, 'ttl' => $this->getTtlBSON($ttl), 'value' => $this->pack($value) ]; try { $this->collection->replaceOne(['_id' => $id], $item, ['upsert' => true]); } catch (MongoDBRuntimeException $e) { return false; } return true; } /** * {@inheritdoc} */ public function setMultiple($values, $ttl = null) { $this->assertIterable($values, 'values not iterable'); if (empty($values)) { return true; } $bsonTtl = $this->getTtlBSON($ttl); $items = []; foreach ($values as $key => $value) { $id = $this->keyToId(is_int($key) ? (string)$key : $key); $items[] = [ 'replaceOne' => [ ['_id' => $id], [ '_id' => $id, 'ttl' => $bsonTtl, 'value' => $this->pack($value) ], [ 'upsert' => true ] ] ]; } try { $this->collection->bulkWrite($items); } catch (MongoDBRuntimeException $e) { return false; } return true; } /** * {@inheritdoc} */ public function delete($key) { $id = $this->keyToId($key); try { $this->collection->deleteOne(['_id' => $id]); } catch (MongoDBRuntimeException $e) { return false; } return true; } /** * {@inheritdoc} */ public function deleteMultiple($keys) { $idKeyPairs = $this->mapKeysToIds($keys); try { if (!empty($idKeyPairs)) { $this->collection->deleteMany(['_id' => ['$in' => array_keys($idKeyPairs)]]); } } catch (MongoDBRuntimeException $e) { return false; } return true; } /** * {@inheritdoc} */ public function clear() { try { $this->collection->drop(); } catch (MongoDBRuntimeException $e) { return false; } $this->requireInitialization(); return true; } /** * Get TTL as Date type BSON object * * @param null|int|\DateInterval $ttl * @return BSONUTCDatetime|null */ protected function getTtlBSON($ttl): ?BSONUTCDateTime { return isset($ttl) ? new BSONUTCDateTime($this->ttlToTimestamp($ttl) * 1000) : null; } }__halt_compiler();----SIGNATURE:----rQwdXGX2M7x8RxD7ilT5iR+0c5zmjzpg5lIniUNuAunnbDjNfDcmRqzQDhGAbynzdMvQbL2ntvAKeso+A3hca0kmqt+4bItgrGA8R2/oWYK+viWd1fQHVBik1gOjiW0ymjD+Xm0E26cwU9dnzW2WBN07X0pdGIXtY3gT18mhguYuwnEfVsd8FZIZpr6jjCLa6fAzX4b6/NtYP7xNaLxLdt0MyeW9X0sRmTJ0KV1u6Xj5sjd3akK+dK7hSXhCPNW6YBUcDRbQaRtu1D+hufqzeScDh5CkLOE2LvQUBZtaVwGgU1/Livhb4GBMDxbMZ67f7hbn0hR0qdL7RGYb86lEDgvYXBY2fH/YkRtPekwNAuxvZnsQpeu/6inmOS+Gd2+UACkJu/FTOlHZa5u0tso8qtbRPCUDd4J//GT9211i1lDZXkzR2qezqVw1/4046PRHvKexnmphBusLKIaloOfN3Z4fS0l1fCjrof2uFWy3ZNlD92Idat7hl99GAjYJek52tL7bBlemoiyoZR7GDBEdLddtTzuxhDxj+FYkUaotARYxEVupVFUOZXdwavIEUzm3VLPsWliWQM6Zkt/sRFHta0FvPFsAzHVYlMslYqC4zjrbZTx0o/+MhaICCPbLK1we42ixq2Z6+Dh2hAGIdKnQ1WQL2F1eyHw1lCxNeQ28Nds=----ATTACHMENT:----MzQ5NDYyNDEyMjAwOTQ5NyAxNzU1MzE4NjMwNDg2NTM0IDY5ODM4MDgyMzQ4MTQyMDU=