C#数据库本地缓存技术(Database local cache)
作者:C/S框架网  发布日期:2011/06/16 22:47:00
C#数据库本地缓存技术(Database local cache)


Database local cache

By konamiman34

A C# class that uses the local file system to cache binary objects stored in a database.

一个C#类用于存储数据库上的二进制对象(数据)保存为本地文件。

类的主要方法:

SaveObject:

As for the public methods exposed by the class, these are the ones:

  • SaveObject: Will store in the database the specified byte array or the contents of the specified file, with the specified name or with the same name as the file itself (there are three method overloads). If there is already an object with the same name in the database, its contents will be overwritten. This method does not access the local cache at all.
  • GetObject: Will retrieve an object from either the database or the local cache, following the algorithm explained above. Returns the path to the cached file, or null if the object with the specified name does not exist in the database.
  • GetCachedFile: Will return the path of the cached file for the specified object name, if it exists, or null otherwise, without accessing the database at all. This method can be used as a "life vest" if the database becomes unreachable, but only if it is acceptable to use data that may be out of date.
  • DeleteObject: Deletes the object with the specified name from both the database and the local cache, if it exists.
  • RenameObject: Changes the name of an object in both the database and the local cache, if it exists.
  • ObjectExists: Returns a boolean value indicating whether an object exists in the database with the specified name or not.
  • GetObjectNames: Returns an array of strings with the names of all the objects stored in the database.
  • PurgeCache: Deletes from the local cache all the files that have no matching object in the database. If the database is accessed by multiple users, it may be convenient to execute this method once when the application execution begins and/or ends.

Note that you can, at any time, manually delete some or all of the cached files, besides/instead of using the PurgeCache method. The class does not maintain any state information about the cached files, it only searches for existing files as needed.

Here are some simple examples of how to use the class. For example, for creating an instance, having the database table named Photos and using C:\PhotosCache as the cache directory, you would do:


SqlConnection connection=new SqlConnection(@"Data Source" +

     @"=.\SQLEXPRESS;AttachDbFilename="|DataDirectory" +

     @"|Data\MyDatabase.mdf";Integrated security=true;");

DatabaseFileCache cache=new DatabaseFileCache(connection, "Photos", @"C:\PhotosCache");



Then, to store objects in the database:


cache.SaveObject(@"C:\Photos\DSCF0100.jpg", "Kaito’s first birtday.jpg");

// Using an alternative method overload:

cache.SaveObject(new byte[] { 1, 3, 5, 7, 11 }, "Some primes");



And to retrieve the data:


string filePath=cache.GetObject("Kaito’s first birtday.jpg")

MyForm.MyPictureBox.Load(filePath);

 

filePath=cache.GetObject("Some primes");

byte[] somePrimes=File.ReadAllBytes(filePath);




原文:http://www.codeproject.com/KB/database/DatabaseLocalCache.aspx

www.csframework.com

点击下载附件 点击下载附件 (如下载失败,请邮件通知我们寄回给您,或QQ:23404761留言.)
上一篇 下一篇