Storing arrays/objects in application variables 
Author Message
 Storing arrays/objects in application variables

Hi!

I have read (at aspfaq.com amongst others) that it's not recommended to
store objects in ASP application variables. However, they seem to be
refering to COM-objects, not JScript Object()s.

I want to cache some data (to avoid having to read it from a datafile or
database everytime a page is accesses) in a JScript object (or an array of
objects). Is it safe and/or good practice to do so? It's only a few KBs...

Best regards
Tomas Eklund, Sweden



Tue, 22 Feb 2005 07:29:43 GMT  
 Storing arrays/objects in application variables
IIRC, JScript objects won't go into application or session scope.
someone should confirm or deny this for me, but I believe they don't
have the correct threading model. Put it this way, I never do it.

function String.prototype.r(){// Javascript rot13 en/decipherment. run
me for Atrax's signature
        var a='nopqrstuvwxyz';var b='abcdefghijklm';var j='/:.'+a+b;var
k='/:.'+b+a;var l='';
        for(var
x=0;x<this.length;x++){l+=k.charAt(j.indexOf(this.charAt(x)));}return l;

Quote:
}       alert("uggc://jjj.ernqgurshpxvatznahny.pb.hx/".r()); // Atrax, MVP

2002.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Tue, 22 Feb 2005 11:01:36 GMT  
 Storing arrays/objects in application variables
That's a major disappointment...

So, the workaround would be to convert JScript arrays to strings using the
join method before storing them in application variables, and then split
them when needed? Or would it be better to build a lot of application
variables, where each array item goes into one application variable? Or are
there even better chaching alternatives? Perhaps generating an include file
with the neccesary code to regenerate the data structure?

I must also mention that I have some very complex data structures that I
would like to chache. For example a tree data structure with arrays of
objects containinig arrays of objects containing arrays of ...

Comments on this, please?

Best regards
Tomas Eklund, Sweden

P.S. This is how I mean:

If I have a simple array of objects like:

    aTest[1].header = '<h1>Hello application</h1>';
    aTest[1].body = '<p>This is a test.</p>';
    aTest[1].image = '/images/test.gif';
    aTest[1].number = 5;
    aText[2].header = '<h1>Will this work?</h1>';
    ... etc

I could either store them in one big application variable like this

    // build a long joined string of the entire array
    // must be splitted i two steps afterwards
    // data must not contain the separator characters
    var aTemp = new Array();
    for ( i in aTest )
    {
        aTemp[i] = "";
        for ( j in aTest[i] )
        {
            aTemp[i] += aTest[i][j] + "|";
        }
        aTemp[i] = aTemp[i].slice(0,-1);
    }
    Application("aTest") = aTemp.join("");

Or in several smalll application variables like this

    // making many small application variables
    // of course one must add a few metadata variables as well
    // not doing that here
    for ( i in aTest )
        for ( j in aTest[i] )
            Application('aTest['+i+'].'+j) = aTest[i][j];

Or I could skip application variables alltogether and generate an include
file vith JScript code that regenerates the data structure. Of course, this
would be a challenging task if not done at the same point as where the
original data structure is built. The generated include file would looke
something like this (using the simple example above):

<%
    var aTest = new Array();
    aTest[1].header = '<h1>Hello application</h1>';
    aTest[1].body = '<p>This is a test.</p>';
    aTest[1].image = '/images/test.gif';
    aTest[1].number = 5;
    aText[2].header = '<h1>Will this work?</h1>';
    ... etc
%>



Tue, 22 Feb 2005 17:24:03 GMT  
 Storing arrays/objects in application variables
Hi

Thats what I usually do ( Store arrays as string and re-create )
but I try to avoid doing this...
If this data does not change that much just use an include else
stick with the DB... perhaps re-create the include when the DB gets updated

--
Best Regards
  Vidar Petursson
 ==============================
Microsoft Internet Client & Controls MVP
 ==============================
 http://www.icysoft.com/


 ==============================
  No matter where you go there you are
 ==============================

Quote:
> That's a major disappointment...

> So, the workaround would be to convert JScript arrays to strings using the
> join method before storing them in application variables, and then split
> them when needed? Or would it be better to build a lot of application
> variables, where each array item goes into one application variable? Or
are
> there even better chaching alternatives? Perhaps generating an include
file
> with the neccesary code to regenerate the data structure?

> I must also mention that I have some very complex data structures that I
> would like to chache. For example a tree data structure with arrays of
> objects containinig arrays of objects containing arrays of ...

> Comments on this, please?

> Best regards
> Tomas Eklund, Sweden

> P.S. This is how I mean:

> If I have a simple array of objects like:

>     aTest[1].header = '<h1>Hello application</h1>';
>     aTest[1].body = '<p>This is a test.</p>';
>     aTest[1].image = '/images/test.gif';
>     aTest[1].number = 5;
>     aText[2].header = '<h1>Will this work?</h1>';
>     ... etc

> I could either store them in one big application variable like this

>     // build a long joined string of the entire array
>     // must be splitted i two steps afterwards
>     // data must not contain the separator characters
>     var aTemp = new Array();
>     for ( i in aTest )
>     {
>         aTemp[i] = "";
>         for ( j in aTest[i] )
>         {
>             aTemp[i] += aTest[i][j] + "|";
>         }
>         aTemp[i] = aTemp[i].slice(0,-1);
>     }
>     Application("aTest") = aTemp.join("");

> Or in several smalll application variables like this

>     // making many small application variables
>     // of course one must add a few metadata variables as well
>     // not doing that here
>     for ( i in aTest )
>         for ( j in aTest[i] )
>             Application('aTest['+i+'].'+j) = aTest[i][j];

> Or I could skip application variables alltogether and generate an include
> file vith JScript code that regenerates the data structure. Of course,
this
> would be a challenging task if not done at the same point as where the
> original data structure is built. The generated include file would looke
> something like this (using the simple example above):

> <%
>     var aTest = new Array();
>     aTest[1].header = '<h1>Hello application</h1>';
>     aTest[1].body = '<p>This is a test.</p>';
>     aTest[1].image = '/images/test.gif';
>     aTest[1].number = 5;
>     aText[2].header = '<h1>Will this work?</h1>';
>     ... etc
> %>



Tue, 22 Feb 2005 17:32:07 GMT  
 Storing arrays/objects in application variables

Quote:

> IIRC, JScript objects won't go into application or session scope.
> someone should confirm or deny this for me, but I believe they don't
> have the correct threading model. Put it this way, I never do it.

Actually, you *can* persist simple JScript arrays and objects at session level.  It works for data only.  You can't call methods on a array or an object (if it had defined methods) retrieved from the Session collection.

<html>
<body>
<script runat=server language="jscript">
var myarr = Session("myarr");
var myobj = Session("myobj");
if(!myarr){
  Session("myarr") = new Array("a","b","c");
  var o = new Object();
  o.name = "my name";
  o.foobar = "my foobar property";
  Session("myobj") = o;
  Response.Write("myarr/myobj saved. refresh the page to see them.");

Quote:
}else{

  for(var i in myarr){
    Response.Write(i+"="+myarr[i]+"<br>");
  }
  for(var x in myobj){
    Response.Write(x+"="+myobj[x]+"<br>");
  }
  // the following would throw a catastrophic error ;-)...
  // sortedarr = myarr.sort();
Quote:
}

</script>
</body>
</html>

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Thu, 24 Feb 2005 07:15:31 GMT  
 Storing arrays/objects in application variables
But it only works for session variables, right? I can't get it to work with
application variables... It is really a shame. I only want to cache small
amounts of data (but a pretty complex structure that takes some time to query
and construct)... Now, I can't

Best regards
Tomas Eklund, Sweden

Michael Harris (MVP) wrote

Quote:
> Actually, you *can* persist simple JScript arrays and objects at
> session level.  It works for data only.  You can't call methods on
> a array or an object (if it had defined methods) retrieved from the
> Session collection.
> [...]



Fri, 25 Feb 2005 11:12:13 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Storing arrays in Application object

2. Storing arrays in Application object

3. Object Stored in Application Variable

4. Storing JScript Arrays in a Session Variable

5. storing a 2 dimensional array in a session variable

6. Help! Storing Arrays in Session Variables - problem

7. Storing arrays in a Session Variable

8. Destroying objects stored in Session / Application

9. Destroying objects stored in Session / Application

10. Storing large amounts of data in Application Object

11. Storing objects in collection/array

12. Passing names of arrays stored in session object to function

 

 
Powered by phpBB® Forum Software