<p>I have figured it out, but am not sure if this is the best way so could still use some advice.</p>

<pre><code># Get a unique hash of your search criteria (still figuring this out).
$hash = md5(json_encode($projectCriterea));

# Look up the cache
$cache = craft()-&gt;cache-&gt;get($hash);

# Did we find this search criterea in the cache already? If so, return it.
if ($cache) {
    return $cache;
}

# No cache? Do searchy business
# ...
$projects = $projectCriterea-&gt;find();
# ...

# Save to the cache
if ($cache === false) {
    craft()-&gt;cache-&gt;set($hash, $projects, 604800); # Cache for 1 week
}

return $projects;
</code></pre>

CC BY-SA 3.0
2016-11-10T00:37:17.037Z
2016-11-10T07:33:35.973Z
2016-11-10T07:33:35.973Z
JamesNZ
<p>Two recent sites I’ve built have plugins built to handle filtering through projects based on some categories and related entries.</p>

<p>For example the lookups look similar to this:</p>

<pre><code># Get element criterea for searching projects
$projectCriterea = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$projectCriterea-&gt;section = 'projects';
$projectCriterea-&gt;limit = null;
$projectCriterea-&gt;relatedTo = $relatedElements; # This is an array of categories and related entries.
$projects = $projectCriterea-&gt;find();
</code></pre>

<p>All these lookups aren’t particularly fast, and there aren’t that many different variants. Is there a way I can add some caching into these lookups?</p>

<p>I had a bit of a look into the <a href="https://craftcms.com/classreference/services/CacheService" rel="nofollow noreferrer">CacheService</a> but I'm not sure how to apply that to this.</p>

<p>Thanks</p>

<entries><cache><elementcriteriamodel>
Caching ElementCriteria Lookups In Craft CMS Plugins
# Caching ElementCriteria Lookups In Craft CMS Plugins

(Asked by: username_0 on 2016-11-09)

Two recent sites I’ve built have plugins built to handle filtering through projects based on some categories and related entries.
For example the lookups look similar to this:
# Get element criterea for searching projects
$projectCriterea = craft()->elements->getCriteria(ElementType::Entry);
$projectCriterea->section = 'projects';
$projectCriterea->limit = null;
$projectCriterea->relatedTo = $relatedElements; # This is an array of categories and related entries.
$projects = $projectCriterea->find();

All these lookups aren’t particularly fast, and there aren’t that many different variants. Is there a way I can add some caching into these lookups?
I had a bit of a look into the CacheService but I'm not sure how to apply that to this.
Thanks

**Tags:** entries, cache, elementcriteriamodel


---

## 1 answers

### Answer by username_0 on 2016-11-10. Score: 2 | Accepted answer

I have figured it out, but am not sure if this is the best way so could still use some advice.
# Get a unique hash of your search criteria (still figuring this out).
$hash = md5(json_encode($projectCriterea));

# Look up the cache
$cache = craft()->cache->get($hash);

# Did we find this search criterea in the cache already? If so, return it.
if ($cache) {
    return $cache;
}

# No cache? Do searchy business
# ...
$projects = $projectCriterea->find();
# ...

# Save to the cache
if ($cache === false) {
    craft()->cache->set($hash, $projects, 604800); # Cache for 1 week
}

return $projects;


---

