-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
SessionStorage.cfc
139 lines (117 loc) · 5.72 KB
/
SessionStorage.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<!-----------------------------------------------------------------------
********************************************************************************
Copyright Since 2005 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
www.ortussolutions.com
********************************************************************************
Author : Luis Majano
Date : February 16,2007
Description :
This is a plugin that enables the setting/getting of permanent variables in
the session scope.
getVar(name,default):any
setVar(name,value):void
deleteVar(name):boolean
exists(name):boolean
clearAll():void
getStorage():struct
clearStorage():void
----------------------------------------------------------------------->
<cfcomponent hint="Session Storage plugin. It provides the user with a mechanism for permanent data storage using the session scope."
output="false"
singleton>
<!------------------------------------------- CONSTRUCTOR ------------------------------------------->
<cffunction name="init" access="public" returntype="SessionStorage" output="false">
<cfscript>
// Lock Properties
instance.lockTimeout = 20;
return this;
</cfscript>
</cffunction>
<!------------------------------------------- PUBLIC ------------------------------------------->
<!--- Set a variable --->
<cffunction name="setVar" access="public" returntype="void" hint="Set a new permanent variable." output="false">
<!--- ************************************************************* --->
<cfargument name="name" type="string" required="true" hint="The name of the variable.">
<cfargument name="value" type="any" required="true" hint="The value to set in the variable.">
<!--- ************************************************************* --->
<cfset var storage = getStorage()>
<cflock scope="session" type="exclusive" timeout="#instance.lockTimeout#" throwontimeout="true">
<cfset storage[arguments.name] = arguments.value>
</cflock>
</cffunction>
<!--- Get A Variable --->
<cffunction name="getVar" access="public" returntype="any" hint="Get a new permanent variable. If the variable does not exist. The method returns blank." output="false">
<!--- ************************************************************* --->
<cfargument name="name" type="string" required="true" hint="The variable name to retrieve.">
<cfargument name="default" type="any" required="false" hint="The default value to set. If not used, a blank is returned." default="">
<!--- ************************************************************* --->
<cfset var storage = getStorage()>
<cfset var results = "">
<cflock scope="session" type="readonly" timeout="#instance.lockTimeout#" throwontimeout="true">
<cfscript>
if ( structKeyExists( storage, arguments.name) )
results = storage[arguments.name];
else
results = arguments.default;
</cfscript>
</cflock>
<cfreturn results>
</cffunction>
<!--- Delete a variable --->
<cffunction name="deleteVar" access="public" returntype="boolean" hint="Tries to delete a permanent session var." output="false">
<!--- ************************************************************* --->
<cfargument name="name" type="string" required="true" hint="The variable name to retrieve.">
<!--- ************************************************************* --->
<cfset var results = false>
<cfset var storage = getStorage()>
<cflock scope="session" type="exclusive" timeout="#instance.lockTimeout#" throwontimeout="true">
<cfset results = structdelete(storage, arguments.name, true)>
</cflock>
<cfreturn results>
</cffunction>
<!--- Exists check --->
<cffunction name="exists" access="public" returntype="boolean" hint="Checks wether the permanent variable exists." output="false">
<!--- ************************************************************* --->
<cfargument name="name" type="string" required="true" hint="The variable name to retrieve.">
<!--- ************************************************************* --->
<cfif NOT isDefined("session") OR NOT structKeyExists(session,"cbStorage")>
<cfreturn false>
<cfelse>
<cfreturn structKeyExists( getStorage(), arguments.name)>
</cfif>
</cffunction>
<!--- Clear All From Storage --->
<cffunction name="clearAll" access="public" returntype="void" hint="Clear the entire coldbox session storage" output="false">
<cfset var storage = getStorage()>
<cflock scope="session" type="exclusive" timeout="#instance.lockTimeout#" throwontimeout="true">
<cfset structClear(storage)>
</cflock>
</cffunction>
<!--- Get Storage --->
<cffunction name="getStorage" access="public" returntype="any" hint="Get the entire storage scope" output="false" >
<cfscript>
// Verify Storage Exists
createStorage();
// Return it
return session.cbStorage;
</cfscript>
</cffunction>
<!--- remove Storage --->
<cffunction name="removeStorage" access="public" returntype="void" hint="remove the entire storage scope" output="false" >
<cflock scope="session" type="exclusive" timeout="#instance.lockTimeout#" throwontimeout="true">
<cfset structDelete(session, "cbStorage")>
</cflock>
</cffunction>
<!------------------------------------------- PRIVATE ------------------------------------------->
<!--- Create Storage --->
<cffunction name="createStorage" access="private" returntype="void" hint="Create the session storage scope" output="false" >
<cfif isDefined("session") AND NOT structKeyExists(session, "cbStorage")>
<!--- Create session Storage Scope --->
<cflock scope="session" type="exclusive" timeout="#instance.lockTimeout#" throwontimeout="true">
<cfif not structKeyExists(session, "cbStorage")>
<cfset session["cbStorage"] = structNew()>
</cfif>
</cflock>
</cfif>
</cffunction>
</cfcomponent>