Cache

From Sum-e Wiki

Jump to: navigation, search

Contents

Introduction

Cache.class.php is a simple cache driver. It is a part of LiteEntityLib and Sum_Framework but can be used on it's own.

Configuration

define( 'CACHE_TYPE', 'memcache' ); // as version 1.0 there is only memcache support
define( 'CACHE_HOST', '127.0.0.1' );
define( 'CACHE_PORT', 11211 );
define( 'CACHE_LIFETIME', 12000 ); // in seconds
define( 'CACHE_PREFIX', 'PROJECTNAME' );

Download

Cache.class.php is a part of LiteEntityLib

Example usage

class Products extends Entity
{
	static function Retrieve( $id, $nocache = false )
	{
            if( !$id )
                return null;

	    $cache = new Cache();

            if( $nocache )
                $cache->delete( CACHE_PREFIX ."ShopProductRetrieve{$id}" );

	     if( $nocache || !$object = $cache->get( CACHE_PREFIX ."ShopProductRetrieve{$id}" ) )
	     {
		  $query = "SELECT * FROM product WHERE id = ?";
		  $entity = new Entity();
		  $object = $entity->GetFirstResult( $query, $id, __CLASS__ );

                  if( !$object )
                      return null;

	          $object->images = Product_Image::ProductCollection( $id, $nocache );
                  $object->comments = Product_Comment::ProductCollection( $id );

    	          $object->tax = Tax::Retrieve( $object->tax );
	          $object->vendor = Vendor::Retrieve( $object->vendor );

		  $cache->set( CACHE_PREFIX ."ShopProductRetrieve{$id}", $object, false, CACHE_LIFETIME );
	      }

	      return $object;
	}

}
Personal tools