Blame | Letzte Änderung | Log anzeigen | RSS feed
<com:TContent ID="body"><h1>Cache Models</h1><p>Some values in a database are know to change slower than others. To improveperformance, many developers like to cache often-used data to avoid makingunnecessary trips back to the database. SQLMap provides its own cachingsystem, that you configure through a <tt><cacheModel></tt> element.</p><p>The results from a query Mapped Statement can be cached simply by specifyingthe <tt>cacheModel</tt> parameter in the statement tag (seen above). A cache modelis a configured cache that is defined within your DataMapper configurationfile. Cache models are configured using the <tt>cacheModel</tt> element asfollows:</p><com:TTextHighlighter Language="xml" CssClass="source"><cacheModel id="product-cache" implementation="LRU" ><flushOnExecute statement="insertProduct"/><flushOnExecute statement="updateProduct"/><flushOnExecute statement="deleteProduct"/><property name="CacheSize" value="100"/></cacheModel></com:TTextHighlighter><p>The cache model above will create an instance of a cache named"product-cache" that uses a Least Recently Used (LRU) implementation. Thevalue of the <tt>type</tt> attribute is either a class name, or an alias for oneof the included implementations (see below). The cache will be flushedwhenever the <tt>insertProduct</tt>, <tt>updateProduct</tt>, or <tt>deleteProduct</tt>mapped statements are executed. There can be any number of "flush onexecute" elements specified for a cache. Some cache implementations may needadditional properties, such as the "cache-size" property demonstrated above.In the case of the LRU cache, the size determines the number of entries tostore in the cache. Once a cache model is configured, you can specify thecache model to be used by a mapped statement, for example:</p><com:TTextHighlighter Language="xml" CssClass="source"><statement id="getProductList" cacheModel="product-cache">select * from PRODUCT where PRD_CAT_ID = #value#</statement></com:TTextHighlighter><h1>Cache Implementation</h1><p>The cache model uses a pluggable framework for supporting different types ofcaches. The choice of cache is specified in the "implementation" attributeof the <tt>cacheModel</tt> element as discussed above. The class name specifiedmust be an implementation of the <tt>ISqlMapCache</tt> interface, or one of thetwo aliases discussed below. Further configuration parameters can be passed tothe implementation via the property elements contained within the body of the<tt>cacheModel</tt>. Currently there are 2 implementations included with the SQLMap PHP DataMapper.</p><div class="info"><b class="tip">Info:</b>The cache implementations, LRU and FIFO cache below do not persist acrossrequests. That is, once the request is complete, all cache data is lost.These caches are useful queries that results in the same repeated data duringthe current request.</div><h2>Least Recently Used [LRU] Cache</h2><p>The LRU cache implementation usesan Least Recently Used algorithm to determines how objects are automaticallyremoved from the cache. When the cache becomes over full, the object that wasaccessed least recently will be removed from the cache. This way, if there isa particular object that is often referred to, it will stay in the cache withthe least chance of being removed. The LRU cache makes a good choice forapplications that have patterns of usage where certain objects may be popularto one or more users over a longer period of time (e.g. navigating back andforth between paginated lists, popular search keys etc.).</p><p>The LRU implementation is configured as follows:</p><com:TTextHighlighter Language="xml" CssClass="source"><cacheModel id="product-cache" implementation="LRU" ><flushOnExecute statement="insertProduct"/><flushOnExecute statement="updateProduct"/><flushOnExecute statement="deleteProduct"/><property name="CacheSize" value="100"/></cacheModel></com:TTextHighlighter><p>Only a single property is recognized by the LRU cache implementation. Thisproperty, named <tt>CacheSize</tt> must be set to an integer value representingthe maximum number of objects to hold in the cache at once. An important thingto remember here is that an object can be anything from a single stringinstance to an array of object. So take care not to store too much in yourcache and risk running out of memory.</p><h2>FIFO Cache</h2><p>The FIFO cache implementation uses an First In First Out algorithm todetermines how objects are automatically removed from the cache. When thecache becomes over full, the oldest object will be removed from the cache. TheFIFO cache is good for usage patterns where a particular query will bereferenced a few times in quick succession, but then possibly not for sometime later.</p><p>The FIFO implementation is configured as follows:</p><com:TTextHighlighter Language="xml" CssClass="source"><cacheModel id="product-cache" implementation="FIFO" ><flushOnExecute statement="insertProduct"/><flushOnExecute statement="updateProduct"/><flushOnExecute statement="deleteProduct"/><property name="CacheSize" value="100"/></cacheModel></com:TTextHighlighter><p>Only a single property is recognized by the FIFO cache implementation. Thisproperty, named <tt>CacheSize</tt> must be set to an integer value representingthe maximum number of objects to hold in the cache at once. An important thingto remember here is that an object can be anything from a single Stringinstance to an array of object. So take care not to store too much in yourcache and risk running out of memory.</p></com:TContent>