在cfquery中使用cachedwithin属性 [英] Using cachedwithin attribute inside cfquery

查看:79
本文介绍了在cfquery中使用cachedwithin属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在cfquery中使用cachedwithin属性时,它如何将查询存储在内存中。它是否仅按您分配给查询的名称存储它?例如,如果我在索引页面上缓存了一个小时的查询并将其命名为getPeople,则将在不同页面(或同一页面)上具有相同名称的查询使用缓存的结果,还是使用了更好的逻辑以确定是否是相同的查询?



此外,如果查询中有变量,缓存是否会考虑该变量的值?

解决方案

不仅是名称-您正在运行的确切查询。

 < cfquery name = getPeople cachedwithin =。5 ...> 
从员工订单中按名称
< / cfquery>选择名称

如果您在应用程序的其他任何地方调用此查询,则将获得缓存版本在第一个查询的半天之内。但是这些将在数据库中获取新数据:

 <!--名称不同,相同的SQL:新的缓存查询---> 
< cfquery name = getEmployees cachedwithin =。5 ...>
从员工订单中按名称
< / cfquery>选择名称

<!--不同的SQL,同名:重新定义缓存的查询--->
<!---注意:正如评论中指出的那样,它实际上并没有覆盖同名的旧查询
,而是在缓存中创建了一个新查询。
同名的第一个仍在高速缓存中,等待逐出。 --->
< cfquery name = getPeople cachedwithin =。5 ...>
从员工订单中按名称desc选择名称
< / cfquery>

是的,它的确考虑了变量。如果使用 cfqueryparam (应做的事),数据库将缓存查询计划,即使使用内部缓存,从查询缓存的角度来看,每个具有更改参数的查询都将被视为不同。请注意,这意味着,如果对使用不同参数多次运行的查询使用 cachedwithin ,则会使缓存命中率较低的查询充斥查询缓存。 / p>

When you use the cachedwithin attribute in a cfquery how does it store the query in memory. Does it store it by only the name you assign to the query? For example, if on my index page I cache a query for an hour and name it getPeople will a query with the same name on a different page (or the same page for that matter) use the cached results or does it use some better logic to decide if it is the same query?

Also, if there is a variable in your query does the cache take into account the value of the variable?

解决方案

It's not only the name -- it's the exact query you're running.

<cfquery name="getPeople" cachedwithin=".5" ...>
select name from employee order by name
</cfquery>

If you invoke this same query anywhere else in your app, you'll get the cached version if it's within half a day of the first query. But these will hit the database for fresh data:

<!--- Different name, same SQL: A new cached query --->
<cfquery name="getEmployees" cachedwithin=".5" ...>
select name from employee order by name
</cfquery>

<!--- Different SQL, same name: Redefining the cached query --->
<!--- Note: As pointed out in comments, it's not really overwriting the old query
      of the same name, but making a new one in the cache. The first one by the
      same name is still in the cache, waiting for eviction. --->
<cfquery name="getPeople" cachedwithin=".5" ...>
select name from employee order by name desc
</cfquery>

And yes, it does take a variable into account. If you use cfqueryparam -- which you should be doing -- your database will cache the query plan, but even using cachedwithin, each query with a changed parameter will be treated as different from a query caching perspective. Note that this means if you use cachedwithin on a query that runs many times with different parameters, you'll be flooding your query cache with queries that have low cache hit rates.

这篇关于在cfquery中使用cachedwithin属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆