You can store ZIP and image files in the AppFabric cache

I would like to know if I can store Plist and Images zip files in AppFabric cache? If so, how? Do I need to hide zip files in binary format or in any other format so that it can be saved in the application.

I am considering storing all zip content in the AppFabric cache, so that the performance and scalability of my application improves.

I am developing my web service in .net C #.

+3
source share
2 answers

Yes, you can store these files in AppFabric - the limitation for storing objects in AppFabric is that they are serialisable (or serializable if you are in the USA :-)). How do you turn a file into a serializable object? You turn it into bytes - here is an example that allows you to download zip files from a web page.

<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();
    }
}
+3
source

if the contents of zip files and images are not dynamically created, why not use the iis cache to cache these files. File Cache (IIS 6.0)

0
source

Source: https://habr.com/ru/post/1755603/


All Articles