TextReader in Cache Leaks?

C

Chan

Got in a difficult situation of storing and retrieving TextReader in
Cache, but found no such post yet.

Tried to store a textreader into Cache object in a similar way
illustrated in .NET's SDK doc ("Retrieving Values of Cached Items"):
' -----------------------------
...
Dim CPCache As textreader = CType(Cache("CP"), textreader)
If CPCache Is Nothing Then
...
Dim txtrd As TextReader = ... ' get something to fill the txtrd.
Cache.Insert("CP", txtrd)
' display txtrd's content on the page
divTag.InnerText = "New!!! " & txtrd.ReadtoEnd
Else
divTag.InnerText = "Cache exists! " & CPCache.ReadtoEnd
End If
' -----------------------------

Web page displayed the txtrd content when it ran the first time as:

New!!! ..... (content of txtrd, all normal char as expected)

Then showed:

Cache exists!

It's verified the length of CPCache's content is zero in Cache. But no
error returned.

However, replacing "txtrd" (TextReader) with "str" (string) and
appropriate methods, content of "str" is shown correctly following
"Cache exists! " on the page.

Does anyone have any experience of TextReader in Cache?

Thanks.
 
D

David Browne

Chan said:
Got in a difficult situation of storing and retrieving TextReader in
Cache, but found no such post yet.

Tried to store a textreader into Cache object in a similar way
illustrated in .NET's SDK doc ("Retrieving Values of Cached Items"):
' -----------------------------
...
Dim CPCache As textreader = CType(Cache("CP"), textreader)
If CPCache Is Nothing Then
...
Dim txtrd As TextReader = ... ' get something to fill the txtrd.
Cache.Insert("CP", txtrd)
' display txtrd's content on the page
divTag.InnerText = "New!!! " & txtrd.ReadtoEnd
Else
divTag.InnerText = "Cache exists! " & CPCache.ReadtoEnd
End If
' -----------------------------

Web page displayed the txtrd content when it ran the first time as:

New!!! ..... (content of txtrd, all normal char as expected)

Then showed:

Cache exists!

Once you've read to the end of your TextReader, it's done. You're at the
end. That's all there is.

David
 
S

Sherif ElMetainy

Hello

I think you need to rewind the text reader every time you use it. Anyways I
I don't understand why do you store the text reader in cache, because in
this case you are only storing a pointer to a stream. Caching the content
you read from the textreader may be a better way.

Best regards

Sherif
 
C

Chan

In .Net document, "DataReader" is described as "forward-only", but
"TextReader" isn't.

Is it true that everything named "Reader" are forward-only like
"DataReader"?

Is there any non-forward-only class corresponding to "TextReader"?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top