Showing posts with label cache. Show all posts
Showing posts with label cache. Show all posts

Wednesday, March 28, 2012

The Cache object

Hello!

Is there a way to get to the Cache object in some other way than the
current contex?
I mean - the Cache is independant from the request so I would expect to
see some kind of Cache.Current property or something like
HttpContext.Current, but unfortunately I've been unable to find one.
When it could be usefull is for example a situation when I put some
task to be done every few minutes on the server - independent from the
requests comming in.
The way I do it now is to catch one of the Requests, get the Cache from
there and store the reference to it in some global/static variable.

--
MichalHttpRuntime.Cache

You might need to reference the System.Web assembly, but you can use it for
any type of application, including windows forms, console, web service...

Karl

--
http://www.openmymind.net/

"Mikeon" <mikeon@dotnet.itags.org.icpnet.pl> wrote in message
news:1142928776.985959.270680@dotnet.itags.org.u72g2000cwu.googlegr oups.com...
> Hello!
> Is there a way to get to the Cache object in some other way than the
> current contex?
> I mean - the Cache is independant from the request so I would expect to
> see some kind of Cache.Current property or something like
> HttpContext.Current, but unfortunately I've been unable to find one.
> When it could be usefull is for example a situation when I put some
> task to be done every few minutes on the server - independent from the
> requests comming in.
> The way I do it now is to catch one of the Requests, get the Cache from
> there and store the reference to it in some global/static variable.
> --
> Michal
Hello!

Yep, I'm already referencing it, but! What I need is a way to get the
Cache object without the Context object which is only available during
request processing which is not allways the case when some code on the
server runs as in my example with tasks running on the server every few
minutes. The importatnt part is that the tasks are run even if no one
uses my application i.e.: no one is requesting pages at the moment and
there is some Timer object stored in the Application state which fires
some tasks.

--
Michal
Not sure if you read what I said, but you can use the HttpRuntime.Cache
which IS available even without a context..

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/

"Mikeon" <mikeon@dotnet.itags.org.icpnet.pl> wrote in message
news:1142945004.761815.70410@dotnet.itags.org.z34g2000cwc.googlegro ups.com...
> Hello!
> Yep, I'm already referencing it, but! What I need is a way to get the
> Cache object without the Context object which is only available during
> request processing which is not allways the case when some code on the
> server runs as in my example with tasks running on the server every few
> minutes. The importatnt part is that the tasks are run even if no one
> uses my application i.e.: no one is requesting pages at the moment and
> there is some Timer object stored in the Application state which fires
> some tasks.
> --
> Michal
Thus wrote Mikeon,

> Hello!
> Is there a way to get to the Cache object in some other way than the
> current contex?
> I mean - the Cache is independant from the request so I would expect
> to
> see some kind of Cache.Current property or something like
> HttpContext.Current, but unfortunately I've been unable to find one.
> When it could be usefull is for example a situation when I put some
> task to be done every few minutes on the server - independent from the
> requests comming in.
> The way I do it now is to catch one of the Requests, get the Cache
> from
> there and store the reference to it in some global/static variable.

You can also consider using the Caching Application Block in EL 2.0. The
API is similar to ASP.NET's Cache class.

Cheers,
--
Joerg Jooss
news-reply@dotnet.itags.org.joergjooss.de
Hello!

Oh! I've missed this first line of yours. Of course it is what I was
looking for.
Thank you!

--
Michal

The Cache object...

I need to get some clarification on how this object is being implemented:

1. Global to the entire application (single server) -- is this on a per user session? Meaning Cache for user session A will be different from Cache for user session B?
2. What could cause the Cache values to be unreliable aka -- not available or cleared out (prior to expiration time)?

Thanks, Rob.

Hi,

Cache is global to entire application. It is one and the same for all users e.g shared for all users. Session is the user-specific data storage on the server.

Cache values can be cleared under memory pressure (priority depends on if one is explicitly given when cache item is added), and when AppDomain restarts: application pool restarts, web.config change, multiple file operations, maximum number of recompiles etc etc

Therefore you should check cache item for null reference before trying to access it. Steve Smith ahs written excellent article about ASP.NET caching (it's old but still valid):http://msdn2.microsoft.com/en-us/library/aa478965.aspx


Thank you for the response.

So if I were to add something like TextBox.ClientID (as the key) to the Cache, it would be possible for another user session to overwrite or get that same Cache item if they used the same TextBox.ClientID?

Rob.


Yes because it it is essentially one and the same storage for all users.

The Cache object

Hello!
Is there a way to get to the Cache object in some other way than the
current contex?
I mean - the Cache is independant from the request so I would expect to
see some kind of Cache.Current property or something like
HttpContext.Current, but unfortunately I've been unable to find one.
When it could be usefull is for example a situation when I put some
task to be done every few minutes on the server - independent from the
requests comming in.
The way I do it now is to catch one of the Requests, get the Cache from
there and store the reference to it in some global/static variable.
MichalHttpRuntime.Cache
You might need to reference the System.Web assembly, but you can use it for
any type of application, including windows forms, console, web service...
Karl
http://www.openmymind.net/
"Mikeon" <mikeon@dotnet.itags.org.icpnet.pl> wrote in message
news:1142928776.985959.270680@dotnet.itags.org.u72g2000cwu.googlegroups.com...
> Hello!
> Is there a way to get to the Cache object in some other way than the
> current contex?
> I mean - the Cache is independant from the request so I would expect to
> see some kind of Cache.Current property or something like
> HttpContext.Current, but unfortunately I've been unable to find one.
> When it could be usefull is for example a situation when I put some
> task to be done every few minutes on the server - independent from the
> requests comming in.
> The way I do it now is to catch one of the Requests, get the Cache from
> there and store the reference to it in some global/static variable.
> --
> Michal
>
Hello!
Yep, I'm already referencing it, but! What I need is a way to get the
Cache object without the Context object which is only available during
request processing which is not allways the case when some code on the
server runs as in my example with tasks running on the server every few
minutes. The importatnt part is that the tasks are run even if no one
uses my application i.e.: no one is requesting pages at the moment and
there is some Timer object stored in the Application state which fires
some tasks.
Michal
Not sure if you read what I said, but you can use the HttpRuntime.Cache
which IS available even without a context..
Karl
http://www.openmymind.net/
http://www.fuelindustries.com/
"Mikeon" <mikeon@dotnet.itags.org.icpnet.pl> wrote in message
news:1142945004.761815.70410@dotnet.itags.org.z34g2000cwc.googlegroups.com...
> Hello!
> Yep, I'm already referencing it, but! What I need is a way to get the
> Cache object without the Context object which is only available during
> request processing which is not allways the case when some code on the
> server runs as in my example with tasks running on the server every few
> minutes. The importatnt part is that the tasks are run even if no one
> uses my application i.e.: no one is requesting pages at the moment and
> there is some Timer object stored in the Application state which fires
> some tasks.
> --
> Michal
>
Thus wrote Mikeon,

> Hello!
> Is there a way to get to the Cache object in some other way than the
> current contex?
> I mean - the Cache is independant from the request so I would expect
> to
> see some kind of Cache.Current property or something like
> HttpContext.Current, but unfortunately I've been unable to find one.
> When it could be usefull is for example a situation when I put some
> task to be done every few minutes on the server - independent from the
> requests comming in.
> The way I do it now is to catch one of the Requests, get the Cache
> from
> there and store the reference to it in some global/static variable.
You can also consider using the Caching Application Block in EL 2.0. The
API is similar to ASP.NET's Cache class.
Cheers,
--
Joerg Jooss
news-reply@dotnet.itags.org.joergjooss.de
Hello!
Oh! I've missed this first line of yours. Of course it is what I was
looking for.
Thank you!
Michal

Tuesday, March 13, 2012

The lifecycle of Cache

Hi all:

I want to use System.Web.Caching.Cache in my web application, but I want to
know the lifecycle of Cache (depending on Page, Session or Application ...).
Thanks!

Enoshre:

Quote:

Originally Posted by

I want to know the lifecycle of Cache (depending on Page, Session or Application ...).


One instance of the Cache class is created per application domain,
and it remains valid as long as the application domain remains active.

You can insert items in the Cache :

1. with a key dependency ( When a key dependency changes, the object becomes invalid
and is removed from the cache ). You can opt to have it replaced with a new key dependency.

2. with a file dependency ( When any dependency changes, the object becomes invalid
and is removed from the cache ). You can opt to have it replaced with a new dependency.

3. With a SQLDependency, based on a SQL Server table or query.
When the data changes, so does the cache.

4. with an absoluteExpiration time

5. with a slidingExpiration time

You can also set up a notification when the item is removed from the cache.

See :
http://msdn2.microsoft.com/en-us/library/4y13wyk9.aspx
http://msdn2.microsoft.com/en-us/li...eb.caching.aspx
http://msdn2.microsoft.com/en-us/li...hing.cache.aspx
http://msdn2.microsoft.com/en-us/li...dependency.aspx
For specific usage, see Cache Insert :
http://msdn2.microsoft.com/en-us/li...che.insert.aspx
There's other types of caching, like Output Caching.
You can control its duration with the "Duration parameter :

<%@. OutputCache Duration="#ofseconds"

See :
http://msdn2.microsoft.com/en-us/library/hdxfb6cy.aspx
You can also use partial caching ( caching user controls, for example ):

See:
http://msdn2.microsoft.com/en-us/li...duration.asp x
http://msdn2.microsoft.com/en-us/li...gattribute.aspx
As you can see, there's a number of answers to your question,
depending on the type of caching you perform.

Read the supplied links and decide which type of caching,
and for which duration, is the caching you want to implement.

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
===================================
"Enosh Chang" <changenosh@.yahoo.com.twwrote in message
news:u5ek0p37GHA.4404@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

Hi all:
>
I want to use System.Web.Caching.Cache in my web application, but I want to know the lifecycle of
Cache (depending on Page, Session or Application ...). Thanks!
>
Enosh
>


Juan's answer is good. Just thought I'd mention that the word you are
looking for is "scope", not "lifecycle".

Karl

--
http://www.openmymind.net/
http://www.codebetter.com/
"Enosh Chang" <changenosh@.yahoo.com.twwrote in message
news:u5ek0p37GHA.4404@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

Hi all:
>
I want to use System.Web.Caching.Cache in my web application, but I want
to know the lifecycle of Cache (depending on Page, Session or Application
...). Thanks!
>
Enosh
>

The lifecycle of Cache

Hi all:
I want to use System.Web.Caching.Cache in my web application, but I want to
know the lifecycle of Cache (depending on Page, Session or Application ...).
Thanks!
Enoshre:
> I want to know the lifecycle of Cache (depending on Page, Session or Application .
.).
One instance of the Cache class is created per application domain,
and it remains valid as long as the application domain remains active.
You can insert items in the Cache :
1. with a key dependency ( When a key dependency changes, the object become
s invalid
and is removed from the cache ). You can opt to have it replaced with a new
key dependency.
2. with a file dependency ( When any dependency changes, the object becomes
invalid
and is removed from the cache ). You can opt to have it replaced with a new
dependency.
3. With a SQLDependency, based on a SQL Server table or query.
When the data changes, so does the cache.
4. with an absoluteExpiration time
5. with a slidingExpiration time
You can also set up a notification when the item is removed from the cache.
See :
http://msdn2.microsoft.com/en-us/library/4y13wyk9.aspx
http://msdn2.microsoft.com/en-us/li...eb.caching.aspx
http://msdn2.microsoft.com/en-us/li...hing.cache.aspx
http://msdn2.microsoft.com/en-us/li...cy.asp
x
For specific usage, see Cache Insert :
[url]http://msdn2.microsoft.com/en-us/library/system.web.caching.cache.insert.aspx[/url
]
There's other types of caching, like Output Caching.
You can control its duration with the "Duration parameter :
<%@. OutputCache Duration="#ofseconds"
See :
http://msdn2.microsoft.com/en-us/library/hdxfb6cy.aspx
You can also use partial caching ( caching user controls, for example ):
See:
http://msdn2.microsoft.com/en-us/li... />
ation.aspx
http://msdn2.microsoft.com/en-us/li...te.asp
x
As you can see, there's a number of answers to your question,
depending on the type of caching you perform.
Read the supplied links and decide which type of caching,
and for which duration, is the caching you want to implement.
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
===================================
"Enosh Chang" <changenosh@.yahoo.com.tw> wrote in message
news:u5ek0p37GHA.4404@.TK2MSFTNGP04.phx.gbl...
> Hi all:
> I want to use System.Web.Caching.Cache in my web application, but I want t
o know the lifecycle of
> Cache (depending on Page, Session or Application ...). Thanks!
> Enosh
>
Juan's answer is good. Just thought I'd mention that the word you are
looking for is "scope", not "lifecycle".
Karl
http://www.openmymind.net/
http://www.codebetter.com/
"Enosh Chang" <changenosh@.yahoo.com.tw> wrote in message
news:u5ek0p37GHA.4404@.TK2MSFTNGP04.phx.gbl...
> Hi all:
> I want to use System.Web.Caching.Cache in my web application, but I want
> to know the lifecycle of Cache (depending on Page, Session or Application
> ...). Thanks!
> Enosh
>