TextReader in Cache Leaks?

  • Thread starter Thread starter Chan
  • Start date Start date
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.
 
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
 
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
 
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!
 
Back
Top