| 1 |
lars |
1 |
<com:TContent ID="body">
|
|
|
2 |
|
|
|
3 |
<h1>Cache Models</h1>
|
|
|
4 |
<p>Some values in a database are know to change slower than others. To improve
|
|
|
5 |
performance, many developers like to cache often-used data to avoid making
|
|
|
6 |
unnecessary trips back to the database. SQLMap provides its own caching
|
|
|
7 |
system, that you configure through a <tt><cacheModel></tt> element.
|
|
|
8 |
</p>
|
|
|
9 |
|
|
|
10 |
<p>The results from a query Mapped Statement can be cached simply by specifying
|
|
|
11 |
the <tt>cacheModel</tt> parameter in the statement tag (seen above). A cache model
|
|
|
12 |
is a configured cache that is defined within your DataMapper configuration
|
|
|
13 |
file. Cache models are configured using the <tt>cacheModel</tt> element as
|
|
|
14 |
follows:</p>
|
|
|
15 |
|
|
|
16 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
17 |
<cacheModel id="product-cache" implementation="LRU" >
|
|
|
18 |
<flushOnExecute statement="insertProduct"/>
|
|
|
19 |
<flushOnExecute statement="updateProduct"/>
|
|
|
20 |
<flushOnExecute statement="deleteProduct"/>
|
|
|
21 |
<property name="CacheSize" value="100"/>
|
|
|
22 |
</cacheModel>
|
|
|
23 |
</com:TTextHighlighter>
|
|
|
24 |
|
|
|
25 |
<p>The cache model above will create an instance of a cache named
|
|
|
26 |
"product-cache" that uses a Least Recently Used (LRU) implementation. The
|
|
|
27 |
value of the <tt>type</tt> attribute is either a class name, or an alias for one
|
|
|
28 |
of the included implementations (see below). The cache will be flushed
|
|
|
29 |
whenever the <tt>insertProduct</tt>, <tt>updateProduct</tt>, or <tt>deleteProduct</tt>
|
|
|
30 |
mapped statements are executed. There can be any number of "flush on
|
|
|
31 |
execute" elements specified for a cache. Some cache implementations may need
|
|
|
32 |
additional properties, such as the "cache-size" property demonstrated above.
|
|
|
33 |
In the case of the LRU cache, the size determines the number of entries to
|
|
|
34 |
store in the cache. Once a cache model is configured, you can specify the
|
|
|
35 |
cache model to be used by a mapped statement, for example:</p>
|
|
|
36 |
|
|
|
37 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
38 |
<statement id="getProductList" cacheModel="product-cache">
|
|
|
39 |
select * from PRODUCT where PRD_CAT_ID = #value#
|
|
|
40 |
</statement>
|
|
|
41 |
</com:TTextHighlighter>
|
|
|
42 |
|
|
|
43 |
<h1>Cache Implementation</h1>
|
|
|
44 |
<p>The cache model uses a pluggable framework for supporting different types of
|
|
|
45 |
caches. The choice of cache is specified in the "implementation" attribute
|
|
|
46 |
of the <tt>cacheModel</tt> element as discussed above. The class name specified
|
|
|
47 |
must be an implementation of the <tt>ISqlMapCache</tt> interface, or one of the
|
|
|
48 |
two aliases discussed below. Further configuration parameters can be passed to
|
|
|
49 |
the implementation via the property elements contained within the body of the
|
|
|
50 |
<tt>cacheModel</tt>. Currently there are 2 implementations included with the SQLMap PHP DataMapper.</p>
|
|
|
51 |
|
|
|
52 |
<div class="info"><b class="tip">Info:</b>
|
|
|
53 |
The cache implementations, LRU and FIFO cache below do not persist across
|
|
|
54 |
requests. That is, once the request is complete, all cache data is lost.
|
|
|
55 |
These caches are useful queries that results in the same repeated data during
|
|
|
56 |
the current request.
|
|
|
57 |
</div>
|
|
|
58 |
|
|
|
59 |
<h2>Least Recently Used [LRU] Cache</h2>
|
|
|
60 |
<p>The LRU cache implementation uses
|
|
|
61 |
an Least Recently Used algorithm to determines how objects are automatically
|
|
|
62 |
removed from the cache. When the cache becomes over full, the object that was
|
|
|
63 |
accessed least recently will be removed from the cache. This way, if there is
|
|
|
64 |
a particular object that is often referred to, it will stay in the cache with
|
|
|
65 |
the least chance of being removed. The LRU cache makes a good choice for
|
|
|
66 |
applications that have patterns of usage where certain objects may be popular
|
|
|
67 |
to one or more users over a longer period of time (e.g. navigating back and
|
|
|
68 |
forth between paginated lists, popular search keys etc.).</p>
|
|
|
69 |
|
|
|
70 |
<p>The LRU implementation is configured as follows:</p>
|
|
|
71 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
72 |
<cacheModel id="product-cache" implementation="LRU" >
|
|
|
73 |
<flushOnExecute statement="insertProduct"/>
|
|
|
74 |
<flushOnExecute statement="updateProduct"/>
|
|
|
75 |
<flushOnExecute statement="deleteProduct"/>
|
|
|
76 |
<property name="CacheSize" value="100"/>
|
|
|
77 |
</cacheModel>
|
|
|
78 |
</com:TTextHighlighter>
|
|
|
79 |
|
|
|
80 |
<p>Only a single property is recognized by the LRU cache implementation. This
|
|
|
81 |
property, named <tt>CacheSize</tt> must be set to an integer value representing
|
|
|
82 |
the maximum number of objects to hold in the cache at once. An important thing
|
|
|
83 |
to remember here is that an object can be anything from a single string
|
|
|
84 |
instance to an array of object. So take care not to store too much in your
|
|
|
85 |
cache and risk running out of memory.</p>
|
|
|
86 |
|
|
|
87 |
<h2>FIFO Cache</h2>
|
|
|
88 |
<p>The FIFO cache implementation uses an First In First Out algorithm to
|
|
|
89 |
determines how objects are automatically removed from the cache. When the
|
|
|
90 |
cache becomes over full, the oldest object will be removed from the cache. The
|
|
|
91 |
FIFO cache is good for usage patterns where a particular query will be
|
|
|
92 |
referenced a few times in quick succession, but then possibly not for some
|
|
|
93 |
time later.</p>
|
|
|
94 |
|
|
|
95 |
<p>The FIFO implementation is configured as follows:</p>
|
|
|
96 |
|
|
|
97 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
98 |
<cacheModel id="product-cache" implementation="FIFO" >
|
|
|
99 |
<flushOnExecute statement="insertProduct"/>
|
|
|
100 |
<flushOnExecute statement="updateProduct"/>
|
|
|
101 |
<flushOnExecute statement="deleteProduct"/>
|
|
|
102 |
<property name="CacheSize" value="100"/>
|
|
|
103 |
</cacheModel>
|
|
|
104 |
</com:TTextHighlighter>
|
|
|
105 |
|
|
|
106 |
<p>Only a single property is recognized by the FIFO cache implementation. This
|
|
|
107 |
property, named <tt>CacheSize</tt> must be set to an integer value representing
|
|
|
108 |
the maximum number of objects to hold in the cache at once. An important thing
|
|
|
109 |
to remember here is that an object can be anything from a single String
|
|
|
110 |
instance to an array of object. So take care not to store too much in your
|
|
|
111 |
cache and risk running out of memory.</p>
|
|
|
112 |
|
|
|
113 |
</com:TContent>
|