CodeFluent Entities: Blob Cache
Blobs in CodeFluent Entities
CodeFluent Entities provides advanced Binary Large Objects (blobs) types from the designer, up to the UI layers. Supported binary types are: video, audio, file, picture and blob. With all these types, the tool defines optional metadatas such as FileName, ContentType, Attributes, Size, LastWriteTime, LastAccessTime and CreationTime.
For all these types, CodeFluent Entities provides built-in support for:
- Streaming: the runtime classes provided support streaming from any client type (web, smart client, WCF) to the server. In the middle tier, the generated code never ever stores the full array of bytes in memory, this way avoiding the deadly Large Object Heap (LOH). Regarding the persistence tier, the data reader uses the SequentialAccess CommandBehavior, and its likewise in the web layer, the code and controls provided use the TransmitFile method, etc.
- Chunking: CodeFluent Entities generates a reusable BITS server (supporting the BITS protocol 1.4 for download and upload, without dependency on IIS).
- Displaying: the CodeFluent Entities web runtime provides ASP.NET custom controls to view, download and upload these blobs sparing developers the extra-work of writing the needed code. More than just ASP.NET, the runtimes also provide TypeConverters and WPF ValueConverters to ease data-binding in .NET applications.
- Caching: blobs can be cached on the server and on the client to relieve the database, a feature which we’ll detail this post.
Generated blobs automatically cache themselves by default. In practice, when loading a blob property using CodeFluent Entities, it actually loads it from the database the first time and stores it on the disk; consequently, next calls won’t load it back from database anymore, unless a modification was done on the blob which will reload it and update the cache.
By default, blobs are cached in a directory in the user profile corresponding to the user running the application. This can be configured at run-time through the configuration file (app.config / web.config) of your application:
<configuration> <configSections> <section name="Sample" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime"/> </configSections> <Sample cacheDirectoryPath="C:\Blobs" /> </configuration>
Note: Sample is my application name and default namespace in CodeFluent Entities.
Please note, that you can enable/disable this server caching feature whenever you want to from the configuration file:
<configuration> <configSections> <section name="Sample" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime"/> </configSections> <Sample cacheEnabled="false" /> </configuration>
In ASP.NET applications, using the generated controls, once the blob was retrieved once, the browser will store it in its temporary files.
Then, on the second call, the browser will ask the blob once again whilst indicating that it already has a version of it in its temporary files by passing the date of it in the HTTP request header. The server will then send back a 304 response indicating to the navigator that the image does not need to be downloaded once again. Ergo, the image stored in the browser’s temporary files will be used.
Thanks to this mechanism, the image was downloaded once and not twice, the database was not called, and IIS served the file using http.sys kernel cache, due to the fact the CodeFluent Runtime uses the TransmitFile function (http://support.microsoft.com/kb/823409) 🙂
In conclusion, this is once again one of those little things, yet so important, that CodeFluent Entities does for us without having us to think about it, so we can focus elsewhere!