Yii Framework Forum: Limited FileCache for tmpfs memory caching - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Limited FileCache for tmpfs memory caching Rate Topic: -----

#1 User is offline   SeaEagle1 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 07-January 17

Posted 07 January 2017 - 08:02 PM

Something I came up with for my small-scale yii2 installation and figured might be useful for others as well: an extension of the standard FileCache which limits the total cache-size. This way it can be used for a shared memory cache (by setting /dev/shm or another tmpfs mount as cachePath), without having to worry about running out of memory due to excessive caching.

Usage is quite simple, just call it in your configuration:
        'cache' => [
            'class' => 'app\helpers\SizeLimitFileCache',
            'cachePath' => '/dev/shm/yiicache',
            'maxSize' => 10000000

Internally it inherits most of the standard FileCache, except instead of random garbage collection, upon storing a new value it checks total cache size. If cache size approaches the set limit, expired items get automatically purged. If after that it's still running out of space, the oldest cache items get purged until enough space has been made available.

Attached File(s)


#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,225
  • Joined: 17-January 09
  • Location:Russia

Posted 08 January 2017 - 01:45 AM

Good one. How about releasing it as an extension?
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark

Share this topic:

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users