可以将zip文件和图像存储在AppFabric缓存中

时间:2010-07-20 20:00:35

标签: file zip appfabric

我想知道,如果我可以将Plist和Images的zip文件存储在AppFabric缓存中?如果有,怎么样?我们是否需要将zip文件转换为二进制格式或其他格式,以便将其存储在App Fabric中。

我正在考虑将整个zip内容存储在AppFabric缓存中,以便提高应用程序的性能和可扩展性。

我正在.net c#中开发我的网络服务。

2 个答案:

答案 0 :(得分:3)

是的,您可以将这些文件存储在AppFabric中 - 在AppFabric中存储对象的限制是它们是serialisable(如果您在美国,则为serializable :-))。如何将文件转换为可序列化对象?你把它变成字节 - 这是一个允许你上传带有网页的zip文件的例子。

<asp:FileUpload runat="server" ID="ZipFileUpload" /><br />
<asp:Button runat="server" ID="UploadButton" Text="Upload file to AppFabric" OnClick="UploadButton_Click" />
<hr />
<asp:GridView runat="server" AutoGenerateColumns="false" ID="CachedZipFilesGridview">
    <Columns>
        <asp:BoundField DataField="Key" />
    </Columns>
</asp:GridView>

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindGrid();
        }
    }

    protected void UploadButton_Click(object sender, EventArgs e)
    {
        DataCacheFactory factory;
        DataCache zipCache;
        Byte[] zipArray;

        // Check to see if the user uploaded a zip file
        if (ZipFileUpload.HasFile && ZipFileUpload.PostedFile.FileName.EndsWith(".zip"))
        {
            // Initialise the byte array to the length of the uploaded file
            zipArray = new Byte[ZipFileUpload.PostedFile.ContentLength];

            // Read the uploaded file into the byte array
            ZipFileUpload.PostedFile.InputStream.Read(zipArray, 0, ZipFileUpload.PostedFile.ContentLength);

            factory = new DataCacheFactory();

            // Get the "files" cache
            zipCache = factory.GetCache("files");

            // Add the byte array to the zipfiles region of the cache
            // Using regions allows us to separate out images and zips
            zipCache.Add(ZipFileUpload.PostedFile.FileName, zipArray,new TimeSpan(1,0,0), "zipfiles");

            bindGrid();
        }
    }

    protected void bindGrid()
    {
        DataCacheFactory factory;
        DataCache zipCache;
        IEnumerable<KeyValuePair<string, object>> cachedFiles;
        DataTable cachedFilesDataTable;

        factory = new DataCacheFactory();

        zipCache = factory.GetCache("files");

        cachedFiles = zipCache.GetObjectsInRegion("zipfiles");

        cachedFilesDataTable = new DataTable();
        cachedFilesDataTable.Columns.Add(new DataColumn("Key", typeof(string)));

        foreach (KeyValuePair<string, object> cachedFile in cachedFiles)
        {
            cachedFilesDataTable.Rows.Add(cachedFile.Key);
        }

        CachedZipFilesGridview.DataSource = cachedFilesDataTable;
        CachedZipFilesGridview.DataBind();
    }
}

答案 1 :(得分:0)