<p>You want to take a look at the <code>CacheService</code> class which is the service that allows you to programmatically interact with the internal cache.</p>

<p>This is an example of how to set some cache that expires in 3600 seconds (one hour). Set the expiration to 0 if you never want it to expire.</p>

<pre><code>craft()-&gt;cache-&gt;set('some_key', 'some_value', 3600);
</code></pre>

<p>Just checkout <code>CacheService</code> for more details, as there are more methods and parameters to make use of, but this should be the basic use case.</p>

<p><a href="https://docs.craftcms.com/api/v2/craft-cacheservice.html" rel="nofollow noreferrer">CacheService Class Reference</a></p>

CC BY-SA 4.0
2015-01-14T16:57:57.933Z
2019-03-13T22:12:43.953Z
2019-03-13T22:12:43.953Z
Justin Kimbrell
<p>I have a plugin that makes several and potentially slow database calls that eventually return 8 Entries. Is there a way to make use of Craft's built in caching on the plugin side? Using the {% cache %} tag doesn't seem to be doing the trick. </p>

CC BY-SA 3.0
2015-01-14T16:55:37.617Z
Are you referring to caching from one of your plugin's templates or caching from PHP code?
Brad Bell
CC BY-SA 3.0
2015-01-14T20:52:16.450Z
Caching from PHP code. The plugin does not have a template, just returns a set of Entries from a variable.
Aaron Berkowitz
CC BY-SA 3.0
2015-01-14T20:53:06.257Z
Then Justin's [answer](http://craftcms.stackexchange.com/a/6585/57) is spot on.
Brad Bell
<plugin-development><cache>
Cacheing in PHP code for plugin
# Cacheing in PHP code for plugin

(Asked by: username_0 on 2015-01-14)

I have a plugin that makes several and potentially slow database calls that eventually return 8 Entries. Is there a way to make use of Craft's built in caching on the plugin side? Using the {% cache %} tag doesn't seem to be doing the trick.

**Tags:** plugin-development, cache

> **Comments:**
> **username_1**: Are you referring to caching from one of your plugin's templates or caching from PHP code?  
> **username_0**: Caching from PHP code. The plugin does not have a template, just returns a set of Entries from a variable.  
> **username_1**: Then Justin's [answer](http://craftcms.stackexchange.com/a/6585/57) is spot on.  

---

## 1 answers

### Answer by username_2 on 2015-01-14. Score: 8 | Accepted answer

You want to take a look at the CacheService class which is the service that allows you to programmatically interact with the internal cache.
This is an example of how to set some cache that expires in 3600 seconds (one hour). Set the expiration to 0 if you never want it to expire.
craft()->cache->set('some_key', 'some_value', 3600);

Just checkout CacheService for more details, as there are more methods and parameters to make use of, but this should be the basic use case.
CacheService Class Reference


---

