# Cache always uses 1 hour duration?

**URL:** <https://discourse.cakephp.org/t/cache-always-uses-1-hour-duration/4480>\
**Category:** Need Help\
**Created:** [June 26, 2018, 3:07pm UTC](https://discourse.cakephp.org/t/cache-always-uses-1-hour-duration/4480 "2018-06-26T15:07:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [June 26, 2018, 3:07pm UTC](https://discourse.cakephp.org/t/cache-always-uses-1-hour-duration/4480/1 "2018-06-26T15:07:39Z")

</div>

I’m trying to save something in the cache using the `Cache` class so that I don’t accidentally send thousands of requests to the Twitch API.

For this, i have created the following code:

```php
$config = Cache::engine("default");
$config->config("duration", "5 minutes");
Cache::config('twitch_channel_info', $config);

/* some other code that sends the request */

Cache::write('twitch_channel_info', ['my dope af data'],'twitch_channel_info');

```

It writes to the cache just fine, but the expiry date…  
that’s 1 hour in the future, not 5 minutes.  
How can I fix this issue?

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [June 26, 2018, 3:48pm UTC](https://discourse.cakephp.org/t/cache-always-uses-1-hour-duration/4480/2 "2018-06-26T15:48:23Z")

</div>

[https://book.cakephp.org/3.0/en/core-libraries/caching.html#configuring-cache-engines](https://book.cakephp.org/3.0/en/core-libraries/caching.html#configuring-cache-engines)

> You can also configure Cache engines at runtime:
> 
> ```auto
> Cache::config('short', [
> 'className' => 'File',
> 'duration' => '+1 hours',
> 'path' => CACHE,
> 'prefix' => 'cake_short_'
> ]);
> 
> ```

so just

```auto
Cache::config('twitch_channel_info', ['duration' => '+5 minutes']);

```

should be enough, but if its always the same why not just set it in `app.php` from the start

---

<div class="post-metadata">

**Author:** ![Schlaefer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/schlaefer/32/1001_2.png) [@Schlaefer](https://discourse.cakephp.org/u/Schlaefer)\
**Post date:** [June 26, 2018, 3:57pm UTC](https://discourse.cakephp.org/t/cache-always-uses-1-hour-duration/4480/3 "2018-06-26T15:57:59Z")

</div>

Usually you configure cache engines as Graziel pointed out. But you might found bug here, where setting the duration in a human readable format later on isn’t converted into (int)seconds.

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [June 26, 2018, 4:08pm UTC](https://discourse.cakephp.org/t/cache-always-uses-1-hour-duration/4480/4 "2018-06-26T16:08:26Z")

</div>

> [@Graziel](#):
>
> Cache::config(‘twitch\_channel\_info’, [‘duration’ =\> ‘+5 minutes’]);

Using just this gives me an error:

```auto
The "twitch_channel_info" cache configuration does not exist

```

I have now specified it in the `app.php` and that does work:

```php
'twitch_channel_info' => [
            'className' => 'File',
            'path' => CACHE,
            'serialize' => true,
            'duration' => '+5 minutes'
]

```

But I refrained from using `app.php` for this as that includes my sensitive data as well, so that doesn’t get committed… but I realised there was a `app.default.php` which I can modify as well for this.

---

<div class="post-metadata">

**Author:** ![Schlaefer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/schlaefer/32/1001_2.png) [@Schlaefer](https://discourse.cakephp.org/u/Schlaefer)\
**Post date:** [June 26, 2018, 7:32pm UTC](https://discourse.cakephp.org/t/cache-always-uses-1-hour-duration/4480/5 "2018-06-26T19:32:46Z")

</div>

app.default.php purpose is more that of an template. Of course it’s possible to read as many custom config files as you want, but if you worry about putting confidential credentials into them you should check out .env files: [https://book.cakephp.org/3.0/en/development/configuration.html#environment-variables](https://book.cakephp.org/3.0/en/development/configuration.html#environment-variables)

There’s already a outcommented section at the top of config/bootstrap.php that gets you started.
