C#数据库本地缓存技术(Database local cache)
Database local cache
By konamiman34A 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, ornullif 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, ornullotherwise, 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 译
C# 本地图片缓存器ImageFileCache (www.csframework.com)
MySqlConnector.MySqlException:“Loading local data is disabled|C/S开发框架
快照隔离事务访问数据库,因为在此数据库中不允许快照隔离。请使用 ALTER DATABASE 以允许快照隔离。
CSFramework.DB 增加 IDatabaseExtensions扩展类|C/S开发框架
DatabaseProvider类详解|C/S开发框架
DataDictCache | 全局缓存设计逻辑详解|C/S开发框架
C/S快速开发框架旗舰版V5.1 | IDatabase接口使用方式|软件手册
C/S快速开发框架旗舰版V5.1 | dalBase与IDatabase关系|软件手册
C/S快速开发框架 | 旗舰版V5.1 | DAL继承关系与IDatabase关系|软件手册
本地文件日志,LocalLog.cs|C#源码
测试CSFramework.DB数据底层组件IDatabase.GetTop方法(Mssql/MySql)
C/S框架日志管理管理器(LogUserOperate)/本地日志(LogLocalException)/数据库日志(LogDB)
CSFramework.DB多数据底层类库(IDatabase/DbDataUpdate)接口测试报告
IDatabase.ExecuteReader返回对象列表Func泛型函数的使用方法
在前缀“http://localhost:19893/”上侦听失败,因为它与计算机上的现有注册冲突
Entity Framework(EF)测试DbContext.Database.ExecuteSqlCommand方法删除记录
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 |
在线客服
