protected function getCursor() : Traversable
{
// Returns original (non-cached) cursor
if ($this->ignoreCache || $this->position >= self::DOCUMENT_LIMIT) {
return $this->getOriginalCursor();
}
// Returns cached set of documents
if ($this->documents) {
return $this->documents;
}
// Check if there is a cached set of documents
$cacheComponent = Ioc::make(CacheComponentInterface::class);
$cacheKey = $this->generateCacheKey();
if ($this->documents = $cacheComponent->get($cacheKey, null)) {
return $this->documents = new ArrayIterator($this->documents);
}
// Stores the original "limit" clause of the query
$this->storeOriginalLimit();
// Stores the documents within the object and cache then for later use
$this->documents = [];
foreach (parent::getCursor() as $document) {
$this->documents[] = $document;
}
$cacheComponent->put($cacheKey, $this->documents, 0.6);
// Drops the unserializable DriverCursor.
$this->cursor = null;
// Return the documents iterator
return $this->documents = new ArrayIterator($this->documents);
}