As far as I know from The Definitive Guide of Yii, Active Record doesn’t support First and Second-Level Cache like Hibernate Framework. I would like to know whether First and Second-Level Cache support will be added in future, and in which release will it be added?
It will be really nice if Active Record supports Second-Level Cache out of the box. All we have to do is just to configure a Distributed Caching System like memcached for the Second-Level caching.
First level cache is the cache to the session/connection. If queries from the same session/connection read the same row of data twice, only one real read will be send to database, the second read will be from the first level cache. In the case of PHP, first level cache is applied to a single request, since each request is a new process and have a new connection to database. So the lifetime of first level cache is same as the lifetime of the request (or database connection).
Second level cache is shared by all sessions/connections. If queries from two or more sessions/connections read the same row of data twice, it will read from first level cache, if cache-miss then read from second level cache, if cache-miss again then read from database. After reading from database, the row of data will be cached in first and second level cache, until memory full and oldest record get removed away. In the case of PHP, second level cache is applied to all requests.
First level cache can be implemented by storing the row in array or so, but the second level cache need to be done with cache provider. Then it is possible to configure a cache provider just like the current cache component in Yii. If we choose a distribute cache like memcached, then we can form a cluster of cached servers to develop a highly scalable web application.