变量值在ColdFusion中的请求之间变化(Variable values changing between requests in ColdFusion)

编程入门 行业动态 更新时间:2024-10-27 01:37:06
变量值在ColdFusion中的请求之间变化(Variable values changing between requests in ColdFusion)

请考虑以下代码:

<cfset local.quiz = getQuiz(param1,param2) /> <!--- returns a struct that has a key called unlock_at ---> <cfset quiz.unlock_at = (quiz.unlock_at EQ '') ? '' : DateConvert('utc2Local',createODBCDateTime(ISOToDateTime(quiz.unlock_at))) />

这就是我原创编写代码的方式。 当我多次调用运行此代码的页面时, quiz.unlock_at的值以6小时为增量(函数中的调整量)发生变化。 当我说增量时我的意思是第一页加载时间是说12:00。 下一页加载6:00,下一页加载00:00,依此类推。 每次物理时间都在变化。 我将代码更改为:

<cfset local.unlock_at = (quiz.unlock_at EQ '') ? '' : DateConvert('utc2Local',createODBCDateTime(ISOToDateTime('#quiz.unlock_at#'))) />

第一件事是我将值存储在局部变量中,不再更新现有的struct。 第二件事是我将日期作为字符串传递给函数而不是变量。 我不确定它是如何改变的。 在任一情况下:

请求之间如何变化。 我不认为这些变量存在于页面请求之间。 所有这些代码都位于CFC内部的函数内。 这些数据中的任何内容都是持久的。 我打了几次电话,看了日期变化然后打开了一个新的浏览器,数据也被改变了。 怎么可能?

这是一个范围问题,组件的数据持久性问题? 告诉我一个循环,我仍然不确定发生了什么,直到我这样做,我担心我可能还有其他问题。

Consider the following code:

<cfset local.quiz = getQuiz(param1,param2) /> <!--- returns a struct that has a key called unlock_at ---> <cfset quiz.unlock_at = (quiz.unlock_at EQ '') ? '' : DateConvert('utc2Local',createODBCDateTime(ISOToDateTime(quiz.unlock_at))) />

This is how I origionally wrote my code. When I called the page that ran this code multiple times, the value of quiz.unlock_at was changing in 6 hour increments (the amount of adjustment in the function). When I say increments I mean first page load the time was say 12:00. The next page load 6:00, the next page load 00:00 and so on. The physical time was changing each time. I changed the code to this:

<cfset local.unlock_at = (quiz.unlock_at EQ '') ? '' : DateConvert('utc2Local',createODBCDateTime(ISOToDateTime('#quiz.unlock_at#'))) />

The first thing is I'm storing the value in a local variable and no longer updating the existing struct. The second thing is I am passing in the date to the function as a string and not as the variable. I wasn't sure how it was getting altered. In either case:

How the heck was this changing between requests. I didn't think these variables lives between page requests. All of this code is inside a function that lives inside a CFC. What of any of that data is persistent. I called the page a few times and watched the dates change then opened a NEW browser and the data was altered too. How is that possible?

Is this a scoping issue, a data persistence issue with components? Threw me for a loop and I still and not sure what happened and until I do I'm afraid I may have other issues.

最满意答案

我想到了。 哎呀...

函数getQuiz创建了一个结构,然后缓存它。 原因是它是一个API调用,我不希望每次都为不经常更改的数据运行API调用。 所以我在做的是:

<cffunction name="getQuiz"> <cfset local.variable = {} /> <!--- Load Sturct Data ---> <!--- CachePut the variable ---> <cfreturn variable /> </cffunction> <cfset quiz = getQuiz() /> <cfset quiz.unlock_at = 1 /> <!--- At this point I have actually edited a direct reference to the cached variable quiz even though I tried locally scoping it etc.

因此,显然返回结构与将结构传递给函数是一样的,即它通过引用而不是通过值传递结构。 出于某种原因,我认为返回一个结构并没有将它作为引用返回,但它是有道理的,因为这是CF在函数之间传递值的方式。

I figured it out. Grrr...

The function getQuiz created a struct and then cached it. The reason being is that it's an API call and I don't want to have to run an API call every time for data that doesn't change that often. So what I was doing was:

<cffunction name="getQuiz"> <cfset local.variable = {} /> <!--- Load Sturct Data ---> <!--- CachePut the variable ---> <cfreturn variable /> </cffunction> <cfset quiz = getQuiz() /> <cfset quiz.unlock_at = 1 /> <!--- At this point I have actually edited a direct reference to the cached variable quiz even though I tried locally scoping it etc.

So, apparently returning a struct is the same as passing a struct into a function, i.e. it passes the struct by reference not by value. For some reason I thought returning a struct didn't return it as a reference, but it makes sense because that is the way CF passes values between functions.

更多推荐

unlock_at,quiz,变化,电脑培训,计算机培训,IT培训"/> <meta name="description

本文发布于:2023-08-06 11:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448649.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量值   ColdFusion   Variable   requests   changing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!