Hi
I need some help with saving retreiving data from the cache, and how best to structure my code. FYI am working in VS2005/under .NET2 Framework.
Ok, we have a series of reports that get run via a report filter screen, and whilst each report is being generated, it populates a memorystream/streamwriter. This has always been done this way but I am trying to update/improve and generally speed things up.
The variables are declared as follows:
and during processing, lines of data are written to the writer like this:
If the user then selects the 'download' button once the report has displayed, it runs the whole report code again (therefore duplicating time/effort), and then at the end it does the following:
where DownloadRawStre am is written as follows:
I have figured out the saving/retreiving data into/from cache, but cannot figure out how to adapt my existing code to save the completed report in the cache, and only when the user selects the 'download report' button to take the cached data and write it to the output file. I also realise that I will probably need to adapt my download procedure.
FYI There could be more than one person at a time using the reports, and the reports will be different for each user, based on their selections, so the cached data needs to be unique to the user?
As I am new to using the cache (until now usually just use session objects), I was wondering if what I want to achieve is possible.
This is what i have tried to do, but guess i just want someone who knows about this to tell me if I'm on the right path, or if I've gone wrong, how to fix it!
When the button is clicked to download the report:
Any help/advice gratefully received.
Regards
Martin
I need some help with saving retreiving data from the cache, and how best to structure my code. FYI am working in VS2005/under .NET2 Framework.
Ok, we have a series of reports that get run via a report filter screen, and whilst each report is being generated, it populates a memorystream/streamwriter. This has always been done this way but I am trying to update/improve and generally speed things up.
The variables are declared as follows:
Code:
MemoryStream vStream = new MemoryStream(); StreamWriter vWriter = new StreamWriter(vStream);
Code:
vWriteLine = "Job Level, Individuals, Companies, Lower Decile, Lower Quartile, Median, Upper Quartile, Upper Decile, Lower Decile, Lower Quartile, Median, Upper Quartile, Upper Decile"; vWriter.WriteLine(vWriteLine);
Code:
if (ReportType == "DownLoad")
{
vWriter.Flush();
vStream.Seek(0, System.IO.SeekOrigin.Begin);
DownloadRawStream(vStream, "text/plain", "Output.csv");
}
Code:
protected void DownloadRawStream( Stream aSource, string aContentType, string aOutFilename )
{
Response.ContentType = aContentType;
Response.AppendHeader( "Content-Disposition", "attachment; filename=" + aOutFilename + ";");
int vBuffSize = 2048;
byte[] vBuffer = new byte[vBuffSize];
int vBytesRead;
bool vFinished = false;
while( ! vFinished ) {
vBytesRead = aSource.Read( vBuffer, 0, vBuffSize );
if ( vBytesRead > 0 )
{
Response.OutputStream.Write( vBuffer, 0, vBytesRead );
}
vFinished = ( vBytesRead < vBuffSize );
}
Response.End();
}
FYI There could be more than one person at a time using the reports, and the reports will be different for each user, based on their selections, so the cached data needs to be unique to the user?
As I am new to using the cache (until now usually just use session objects), I was wondering if what I want to achieve is possible.
This is what i have tried to do, but guess i just want someone who knows about this to tell me if I'm on the right path, or if I've gone wrong, how to fix it!
Code:
// flush the writer...
vWriter.Flush();
// write to stream...
vStream.Seek(0, System.IO.SeekOrigin.Begin);
// set buffer size...
int vBuffSize = 2048;
// create buffer array...
byte[] vBuffer = new byte[vBuffSize];
// define and initialise marker...
bool vFinished = false;
// output to buffer
while (!vFinished)
{
// get the number of bytes read...
int vBytesRead = vStream.Read(vBuffer, 0, vBuffSize);
// set boolean variable...
vFinished = (vBytesRead < vBuffSize);
}
// try and get the existing report...
byte[] vCache = Cache.Get("Report1") as byte[];
// if found, remove it...
if (vCache != null) Cache.Remove("Report1");
// insert report into cache...
Cache.Insert("Report1", vBuffer);
Code:
// get report from cache...
byte[] vBuffer = Cache.Get("Report1") as byte[];
// output to CSV...
Response.ContentType = "text/plain";
Response.AppendHeader( "Content-Disposition", "attachment; filename=Output.csv;");
Response.OutputStream.Write(vBuffer, 0, vBuffer.Length);
Response.End;
Regards
Martin
Comment