在cfquery中使用cachedwithin attibute [英] Using cachedwithin attibute inside cfquery

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

问题描述

在cfquery中使用cachedwithin属性时,如何将查询存储在内存中。它是否只通过分配给查询的名称存储它?例如,如果在我的索引页面上我缓存一个小时的查询和名称,getPeople将在不同的页面(或相同的页面上)使用相同的名称的查询使用缓存结果或使用一些更好的逻辑



此外,如果在查询中有一个变量,缓存会考虑变量的值吗?

解决方案

它不仅是名称 - 这是你运行的确切查询。

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

如果您在应用程式的任何其他地方调用此同一查询,在第一次查询的半天内。但是这些将命中数据库的新鲜数据:

 <!---不同的名称,相同的SQL: ---> 
< cfquery name =getEmployeescachedwithin =。5...>
按名称从员工订单中选择名称
< / cfquery>

<!---不同的SQL,同名:重新定义缓存的查询--->
<!---注意:正如在注释中指出的,它并不真正覆盖同名的旧查询
,而是在缓存中创建一个新的查询。第一个由
相同的名字仍然在缓存中,等待驱逐。 --->
< cfquery name =getPeoplecachedwithin =。5...>
按名称从雇员订单中选择名称desc
< / cfquery>

是的,它需要考虑一个变量。如果您使用 cfqueryparam (您应该这样做),您的数据库将缓存查询计划,但即使使用 cachedwithin ,每个具有更改参数的查询将被视为与查询缓存透视图不同。请注意,这意味着如果在使用不同参数运行多次的查询中使用 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 attibute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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