Link to home
Start Free TrialLog in
Avatar of andw928
andw928

asked on

Finding keys of a structure using date ranges

I have a structure with a date and time value that looks like this:

<cfoutput>
<cfset storage = structnew()>
<cfset storage["time"] = "{ts '2005-04-26 12:03:50'}">
</cfoutput>

Now I want to use structfindvalue() to find any keys where the time is 3 or more hours from the current time, how would I do this?

<cfset results = structfindvalue(storage, "<!--- what would go here? --->", "ALL")>

What would I replace that comment with inorder to find any keys where the date and time value is 3 or more hours from the current time? Any ideas?
Avatar of Renante Entera
Renante Entera
Flag of Philippines image

Hi andw928!

I just want to clarify something.  Are your comparing it with the current time only not current datetime ???

If you are looking for the keys then you will do something like this :

<cfset storage = structnew()>
<cfset storage["time1"] = "{ts '2005-04-25 16:03:50'}">
<cfset storage["time2"] = "{ts '2005-04-27 18:03:50'}">
<cfset storage["time3"] = "{ts '2005-04-28 15:03:50'}">

<cfset keyList = StructKeyList(storage)>
<cfset newList = ''>

<cfloop index="i" list="#keyList#">
  <cfif Datediff('h',now(),storage[i]) GTE 3>
    <cfset newList = ListAppend(newList,i)>
  </cfif>
</cfloop>

<cfoutput>
Keys having values 3 hours or more from the current datetime #now()# are the ff.:<br>
#ListSort(newList,'text','asc')#
</cfoutput>

:: Reminders ::
   1.  In my code, I'm comparing it with the current datetime.
   2.  If you want to compare it with current time only then replace "<cfif Datediff('h',now(),storage[i]) GTE 3>"
        with "<cfif Datediff('h',TimeFormat(now(),'H:mm:ss'),TimeFormat(storage[i],'H:mm:ss')) GTE 3>"
   3.  And of course, replace the extra label "current datetime #now#" with "current time #TimeFormat(now(),'H:mm:ss')#"

Hope this helps you.  Just try it.


Goodluck!
eNTRANCE2002 :-)
Avatar of andw928
andw928

ASKER

That would be nice, although under some testing and benchmarks, looping the code I have takes alot longer than using structfindvalue(). Is there anyway to specify a datetime range using structfindvalue? That was my question, to replace the comment in this tag, with a datetime range, if at all possible:

<cfset results = structfindvalue(storage, "<!--- what would go here? --->", "ALL")>

And if not possible, then what would be the most efficient and fastest way to remove keys that have datetime values more than 3 hours from now without looping it? I expect to have 1000-2000 keys in the structure, looping it on each page request wouldn't be efficient, plus the structure would be in the application scope, so the data would have to be locked...any ideas?

-Matt

Hmmm ... :-?  I think that could not be possible.  You could not do that using StructFindValue().

Since you have 1000-2000 keys in the structure, it would be better if you will store it in a database.  So, you will have a table that has three(3) columns - col1(for structure), col2(for key), col3(for value).

Then referring to that table, you will do something like this :
1.  Selecting records from table
     <cfquery name="qGetRecord" datasource="DSN">
       Select * From TableName
       Where col1 = 'storage'
       And col2 >= '#DateAdd('h',3,now())#'
     </cfquery>
2.  Retrieving key basing from #1 query
     <cfoutput query="qGetRecord">
       #col2#<br>
     </cfoutput>
3.  Or you want it to store in a list variable
     <cfset keyList = ValueList(qGetRecord.col2)>

Hope this makes sense.


Regards!
eNTRANCE2002 :-)
Avatar of andw928

ASKER

Once again, I don't know about the database, becuase with these structures, there will be a delete, insert, and a select on each page. Also, I'd have to index 2 columns, which will almost be 8-10 bytes each, non-numeric. That would cause extreme overhead.

The object of the application is to completel an extremely fast application, where the page execution time is low.

I was thinking..what if I setup a scheduled task that runs lets say every 10 or so minutes, would that be good? Would it be better? Would there be any disadvantages?

If nobody else gives me any suggestions then I'll give you the points entrance.
Avatar of andw928

ASKER

Wait, no, scheduled tasks are no good, becuase I would need the results immediately not every so and so. But, besides loops and queries, is there any fast and efficient way to do this? Becuase my data will range anywhere from 1 item to 2000. So an insert, select, delete with 2-3 indexes, requested on each page, would definetely not be efficient especially if I only have 1 item.

But, then the application structures would be a good and fast place, but they would be slower if it reaches near 2000 items, then the queries would be faster. Is there anyway I can do this to balance this out?
ASKER CERTIFIED SOLUTION
Avatar of Renante Entera
Renante Entera
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial