LemonStand Forum: File Based Cache Expiring - LemonStand Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

File Based Cache Expiring

#1 User is offline   activeholdingco 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 177
  • Joined: 23-September 10

Posted 06 May 2011 - 06:01 PM

I am heavily using the file based cache on my site and I am wondering why the time to expire does not work on the file based caching. I propose the following solution:

Wrap the dataset that is going to be stored in the cache in an array so it looks like array('data_set'=>$cached_data, 'data_stored_time'=>time(), 'data_expired_time'=>123123);

This way you can check to see if the data_expired_time is past the time used in $cache->set('expire', 'value', 30);

To make it backwards compatible, just do a simple if statement:
if( !isset($data['data_set']) )
    $data = unserialized($data);
else {
    $data = unserialized($data['data_set']);
    do cache invalidation
}


And wallah! We have cache that can invalidate when it is pulled! I might put together some code later.

I really need this as I have some cache that is permanent and will never change and some things like featured products or on sale need to be updated daily or hourly.
0

#2 User is offline   activeholdingco 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 177
  • Joined: 23-September 10

Posted 06 May 2011 - 09:09 PM

Started looking at the code. It looks like you check if the value is not a string or int and serialize it. I dont think there is any reason to do this now. We should just serialize everything along with the time. That would also get rid of the is_serialized function since we already know.

But in order to be backwords compatible we would have to leave it in. You could just invalidate it if its not in the right format and publish a note on the update. The cache would be rebuilt next page load for those who update, but small price to pay for the ability to set TTL on file based caching. It would be much cleaner code without backwards compatibility and not rely upon the filesystem which should make it a little faster.

Ive tested both versions, but my vote would be for the first.

Heres my code for the new cache:
		/**
		 * Adds or updates value to the cache
		 * @param string $key The key that will be associated with the item.
		 * @param mixed $value The variable to store.
		 * @param int $ttl Time To Live
		 * @return bool Returns TRUE on success or FALSE on failure.
		 */
		protected function set_value($key, $value, $ttl = null)
		{
			$value = serialize(array('lsdata'=>$value, 'lscache'=>time(), 'lsttl'=>(int)$ttl));

			...
		}

		protected function get_cache_value($key)
		{
			$dest_path = $this->dir_path.'/'.$key;
			if (!file_exists($dest_path))
				return false;
				
			$contents = file_get_contents($dest_path);
			if (($unserialized = @unserialize($contents)) === false)
				return false;
			
			if( !isset($unserialized['lsdata']) )
				return false;
			
			if ($unserialized['lsttl'] && ((time() - $unserialized['lscache']) > $unserialized['lsttl']))
			{
				@unlink($dest_path);
				return false;
			}
			else if ($this->ttl && ((time() - $unserialized['lscache']) > $this->ttl))
			{
				@unlink($dest_path);
				return false;
			}
				
			return $unserialized['lsdata'];
		}


Heres the backwards compatibility version: (It doesnt check for TTL yet if the object is not serialized).
		/**
		 * Adds or updates value to the cache
		 * @param string $key The key that will be associated with the item.
		 * @param mixed $value The variable to store.
		 * @param int $ttl Time To Live
		 * @return bool Returns TRUE on success or FALSE on failure.
		 */
		protected function set_value($key, $value, $ttl = null)
		{
			$value = serialize(array('lsdata'=>$value, 'lscache'=>time(), 'lsttl'=>(int)$ttl));

			...
		}

		protected function get_cache_value($key)
		{
			$dest_path = $this->dir_path.'/'.$key;
			if (!file_exists($dest_path))
				return false;
				
			$contents = file_get_contents($dest_path);
			
			$unserialized = null;
			if ( !$this->is_serialized($contents, $unserialized) )
				return $contents;

			if( is_array($unserialized) && isset($unserialized['lsdata']) )
			{
				if ($unserialized['lsttl'] && ((time() - $unserialized['lscache']) > $unserialized['lsttl']))
				{
					@unlink($dest_path);
					return false;
				}
				
				return $unserialized['lsdata'];
			}
			else
			{
				if ($this->ttl && ((time() - filemtime($dest_path)) > $this->ttl))
				{
					@unlink($dest_path);
					return false;
				}
				
				return $unserialized;
			}

			return $contents;
		}

0

#3 User is online   Aleksey 

  • Co-Founder
  • Group: +Administrators
  • Posts: 3,626
  • Joined: 31-October 09

Posted 08 May 2011 - 04:20 PM

Hi!

We added it to our internal task list. Thanks!

#4 User is offline   Eric 

  • Developer
  • PipPipPip
  • Group: Members
  • Posts: 1,290
  • Joined: 04-August 10
  • LocationVancouver, Canada

Posted 10 January 2012 - 11:43 AM

Implemented. Refer to tracker: http://forum.lemonst...cache-expiring/
0

Share this topic:


Page 1 of 1

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