Use Mongodb storage to upload physical files and perform SQUID acceleration (based on the aspx page)

Source: Internet
Author: User
Tags mongodb client

When reading the MongoDB GFS document, I learned how to store physical files (including large files. In order to deepen your impression, I wrote an example of uploading files to Mongodb. Of course, since it is stored in the document database, you cannot access these physical files in normal ways, here, An aspx page is specially written to read these files (such as video clips or MP3 files). Therefore, after downloading the example, you will see two pages: uploadfile. aspx (upload), one is getfile. aspx (read the file from mongodb as a stream ). Of course, considering the access speed, SQUID is introduced here for file acceleration (currently SQUID only caches static files by default, so we need to set the output of the ASPX page here, these will be introduced in this article)

First, let's introduce the development environment. I use VS2008 + SP1, And the mongodb client uses samus-mongodb (the latest version)

Add reference to the following namespace

Using MongoDB;
Using MongoDB. GridFS;

 


The following code uploads a file: uploadfile. aspx. cs

HttpPostedFile myFile = FileUpload. PostedFile;
Int nFileLen = myFile. ContentLength;

Byte [] myData = new Byte [nFileLen];
MyFile. InputStream. Read (myData, 0, nFileLen );

GridFile fs = new GridFile (DB, filesystem );

Random random = new Random (unchecked (int) DateTime. Now. Ticks ));
String newfilename = string. format ("{0} {1} {2}", random. next (1000,999 99), random. next (1000,999 99), System. IO. path. getExtension (myFile. fileName ));
GridFileStream gfs = fs. Create (newfilename );
Gfs. Write (myData, 0, nFileLen );
Gfs. Close ();

 


Here we just give a random name to the uploaded file, so that if everything is normal, you can find the file in the database, such:



Next, let's take a look at how to access the uploaded physical file getfile. aspx. cs (by passing the filename parameter, the corresponding field structure in mongodb, for example ):

Protected void Page_Load (object sender, EventArgs e)
{
If (! String. IsNullOrEmpty (Request. QueryString ["filename"])
{
String filename = Request. QueryString ["filename"];
Init ();
String filesystem = "gfstream ";

GridFile fs = new GridFile (DB, filesystem );
GridFileStream gfs = fs. OpenRead (filename );

Byte [] buffer = new Byte [gfs. Length];

HttpContext. Current. Response. AddHeader ("Expires", DateTime. Now. AddDays (20). ToString ("r "));
HttpContext. Current. Response. AddHeader ("Cache-Control", "public ");

// The length of the data to be read
Long dataToRead = gfs. Length;
Int length;
While (dataToRead> 0)
{
// Check whether the client is still connected
If (HttpContext. Current. Response. IsClientConnected)
{
Length = gfs. Read (buffer, 0, 10000 );
HttpContext. Current. Response. OutputStream. Write (buffer, 0, length );
HttpContext. Current. Response. Flush ();
Buffer = new Byte [10000];
DataToRead = dataToRead-length;
}
Else
{
// Jump out of the endless loop if the connection is no longer available
DataToRead =-1;
}
}
Gfs. Dispose ();
HttpContext. Current. Response. End ();
}
}

 


The following shows the final running effect of retrieving the file list from mongodb in a list:



Although MONGODB's concurrent performance is very good, it also suffers performance loss every time it is retrieved from mongodb, especially for infrequently changed physical files, so here
SQUID is used for file caching. Currently, SQUID only supports static files by default. You need to set the stream information output from the ASPX page in this example to cache.

First, if the squid. conf file contains the following lines, you need to use # for comments (it will disable caching all? ):

 

Hierarchy_stoplist cgi-bin? \. Php \. html
Acl QUERY urlpath_regex cgi-bin \? \. Php \. html
Cache deny QUERY

In this way, modify the corresponding. aspx and add the following information to the Header:

HttpContext. Current. Response. AddHeader ("Expires", DateTime. Now. AddDays (20). ToString ("r "));
HttpContext. Current. Response. AddHeader ("Cache-Control", "public ");

 

In this way, SQUID will faithfully cache the corresponding file based on the header information.

Of course, you can also use the following method to make the specified aspx files be squid cached:

Acl CACHABLE_PAGES urlpath_regex \ getfile. aspx
# Allow the aspx page on the cache
No_cache allow CACHABLE_PAGES

 


The following acl matches all dynamic pages and disables cache of all aspx pages.

# Acl NONE_CACHABLE_PAGES urlpath_regex \? \. Aspx
# Disable cache for other aspx pages
# No_cache deny NONE_CACHABLE_PAGES

 


The following lines set the page cache duration. The first line is cache for one day and the second line is cache for two minutes.

Refresh_pattern ^ http: // 10.0.4.114: 1100/mongodbsample/getfile. aspx 1440 0% 1440 ignore-reload
Refresh_pattern ^ http: // 10.0.4.114: 1100/mongodbsample/getfile. aspx 2 0% 2 ignore-reload

 


To configure SQUID correctly, you only need to visit the site requested by SQUID (http: // 10.0.4.85: 8989/mongodbspame/uploadfile. aspx in this article ),
Here it will go to http: // 10.0.4.114: 1100/mongodbspame/uploadfile. aspx to get the page information, and the link getfile. aspx in the page will be
Cache, for example:

Now, the content of today is here first. Download the sample source code and SQUID configuration file:/Files/daizhj/mongodbsample.rar

Link: http://www.cnblogs.com/daizhj/archive/2010/08/19/1803454.html
BLOG: http://daizhj.cnblogs.com/
Author: daizhj, Dai zhenjun




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.