using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.Web.Internal;
public partial class CacheSample : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
public void ASPxGridView1_HtmlDataCellPrepared(object sender, DevExpress.Web.ASPxGridViewTableDataCellEventArgs e) {
if (e.DataColumn.FieldName == "Picture")
(e.Cell.Controls[0] as BinaryImageDisplayControl).Attributes.Add("data-key", e.KeyValue.ToString());
}
protected void ASPxButton1_Click(object sender, EventArgs e) {
Random rnd = new Random();
gaugeControl1.Value = rnd.Next(100).ToString();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="CacheSample" %>
<%@ Register Assembly="DevExpress.Web.ASPxGauges.v17.1" Namespace="DevExpress.Web.ASPxGauges.Gauges" TagPrefix="dx" %>
<%@ Register assembly="DevExpress.Web.ASPxGauges.v17.1" namespace="DevExpress.Web.ASPxGauges" tagprefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxGauges.v17.1" Namespace="DevExpress.Web.ASPxGauges.Gauges.Linear" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxGauges.v17.1" Namespace="DevExpress.Web.ASPxGauges.Gauges.Circular" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxGauges.v17.1" Namespace="DevExpress.Web.ASPxGauges.Gauges.State" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxGauges.v17.1" Namespace="DevExpress.Web.ASPxGauges.Gauges.Digital" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v17.1" Namespace="DevExpress.Web" TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
FileCacheStrategy demonstration:
<br />
<dx:ASPxGaugeControl ID="gaugeControl1" runat="server" BackColor="White" Height="260px"
Value="20" Width="260px">
<Gauges>
<dx:CircularGauge Bounds="0, 0, 260, 260" Name="cGauge1">
<BackgroundLayers>
<dx:ArcScaleBackgroundLayerComponent Name="bg1" ScaleCenterPos="0.5, 0.61" ScaleID="scale1"
Size="250, 205" ZOrder="1000" ShapeType="CircularThreeFourth_Style7"></dx:ArcScaleBackgroundLayerComponent>
</BackgroundLayers>
<Needles>
<dx:ArcScaleNeedleComponent EndOffset="-25" StartOffset="-21" ScaleID="scale1" Name="needle1"
ZOrder="-50" ShapeType="CircularFull_Style7"></dx:ArcScaleNeedleComponent>
</Needles>
<EffectLayers>
<dx:ArcScaleEffectLayerComponent Name="effect1"
Shader="<ShaderObject Type="Opacity" Data="Opacity[0.75]"/>"
ScaleID="scale1" Size="230, 110" ZOrder="-1000" ShapeType="CircularThreeFourth_Style7">
</dx:ArcScaleEffectLayerComponent>
</EffectLayers>
<Scales>
<dx:ArcScaleComponent Name="scale1" MaxValue="100" Value="20" MinorTickmark-ShapeType="Circular_Style7_1"
Center="125, 140" EndAngle="30" MajorTickCount="9" MinorTickCount="4" MajorTickmark-TextOffset="22"
MajorTickmark-TextOrientation="LeftToRight" MajorTickmark-FormatString="{0:F0}"
MajorTickmark-ShapeType="Circular_Style7_2" StartAngle="-210" RadiusX="83" RadiusY="83">
</dx:ArcScaleComponent>
</Scales>
</dx:CircularGauge>
</Gauges>
</dx:ASPxGaugeControl>
<br />
<dx:ASPxButton ID="ASPxButton1" runat="server" OnClick="ASPxButton1_Click" Text="Generate random value">
</dx:ASPxButton>
<br />
<br />
StaticImageStrategy demonstration:
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" OnHtmlDataCellPrepared="ASPxGridView1_HtmlDataCellPrepared"
DataSourceID="AccessDataSource1" KeyFieldName="CategoryID">
<Columns>
<dx:GridViewDataTextColumn FieldName="CategoryID" ReadOnly="True" VisibleIndex="0">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="CategoryName" VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="Description" VisibleIndex="2">
</dx:GridViewDataTextColumn>
<dx:GridViewDataBinaryImageColumn FieldName="Picture" Name="Picture" VisibleIndex="3">
</dx:GridViewDataBinaryImageColumn>
</Columns>
</dx:ASPxGridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/nwind.mdb"
SelectCommand="SELECT * FROM [Categories]"></asp:AccessDataSource>
</form>
</body>
</html>
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e) {
DevExpress.Web.BinaryStorageConfigurator.Mode = DevExpress.Web.BinaryStorageMode.Custom;
DevExpress.Web.BinaryStorageConfigurator.RegisterCustomStorageStrategy(new StaticImageStrategy());
DevExpress.Web.BinaryStorageConfigurator.RegisterStorageStrategy(new FileCacheStrategy(),
SupportsStrategy);
}
private bool SupportsStrategy(DevExpress.Web.ASPxWebControlBase control) {
return control.GetType() == typeof(DevExpress.Web.ASPxGauges.ASPxGaugeControl);
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
using System;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.Web;
using System.IO;
public class FileCacheStrategy : RuntimeStorageStrategy {
public string FileCacheFolderPath = "~/FileCacheFolder/";
public const int CacheClearingInterval = 1; // minutes
public const int CacheExpirationTime = 10; // minutes
public static DateTime LastCacheClearing = DateTime.MinValue;
/* GetResourceKey
1) the mime type is saved as a file's extension for further
use within the GetResourceData method.
2) received key is used to generate the image url for further
use within the StoreResourceData, GetResourceData
*/
public override string GetResourceKey(ASPxWebControlBase control, byte[] content, string mimeType) {
string fileExtension = "";
if(mimeType.StartsWith("image/"))
fileExtension = "." + mimeType.Substring("image/".Length);
return Guid.NewGuid().ToString() + fileExtension;
}
public override void StoreResourceData(ASPxWebControlBase control, string key, BinaryStorageData data) {
ClearFileCacheIfNeeded(GetFolderPath());
using(FileStream stream = new FileStream(GetFilePath(key), FileMode.OpenOrCreate))
stream.Write(data.Content, 0, data.Content.Length);
}
public override BinaryStorageData GetResourceData(string key) {
using(FileStream stream = new FileStream(GetFilePath(key), FileMode.Open)) {
byte[] content = new byte[stream.Length];
stream.Read(content, 0, content.Length);
string mimeType = "image/" + key.Split('.')[1];
return new BinaryStorageData(content, mimeType);
}
return null;
}
private static void ClearFileCacheIfNeeded(string physicalPath) {
DateTime now = DateTime.Now;
// cache clearing is launched after the specified time interval
if(now - LastCacheClearing < TimeSpan.FromMinutes(CacheClearingInterval))
return;
string[] fileNames = Directory.GetFiles(physicalPath);
foreach(string fileName in fileNames) {
if(now - File.GetCreationTime(fileName) > TimeSpan.FromMinutes(CacheExpirationTime)) {
try {
File.Delete(fileName);
} catch { }
}
}
LastCacheClearing = now;
}
private string GetFilePath(string key) {
return GetFolderPath() + key;
}
private string GetFolderPath() {
return HttpContext.Current.Server.MapPath(FileCacheFolderPath);
}
}
// images are cached within the predefined public folder. The method returns an image url.
public class StaticImageStrategy : RuntimeStorageStrategy {
public string StaticCacheFolderName = "StaticCacheFolder"; // public folder
public const int FileExpirationTime = 60; // minutes
public override string GetResourceUrl(ASPxWebControlBase control, byte[] content, string mimeType, string contentDisposition) {
string fileExtension = "";
var a = control.NamingContainer.BindingContainer;
if (mimeType.StartsWith("image/"))
fileExtension = "." + mimeType.Substring("image/".Length);
string fileName = control.Attributes["data-key"] + fileExtension;
string physicalPath = HttpContext.Current.Server.MapPath(string.Format("~/{0}/", StaticCacheFolderName)) + fileName;
if(File.Exists(physicalPath) && DateTime.Now - File.GetCreationTime(physicalPath) > TimeSpan.FromMinutes(FileExpirationTime))
File.Delete(physicalPath);// an image is refreshed every hour
if(!File.Exists(physicalPath)) {
using(FileStream stream = new FileStream(physicalPath, FileMode.OpenOrCreate))
stream.Write(content, 0, content.Length);
}
return String.Format("{0}/{1}", StaticCacheFolderName, fileName);
}
}
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports DevExpress.Web.Internal
Partial Public Class CacheSample
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Public Sub ASPxGridView1_HtmlDataCellPrepared(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxGridViewTableDataCellEventArgs)
If e.DataColumn.FieldName = "Picture" Then
TryCast(e.Cell.Controls(0), BinaryImageDisplayControl).Attributes.Add("data-key", e.KeyValue.ToString())
End If
End Sub
Protected Sub ASPxButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim rnd As New Random()
gaugeControl1.Value = rnd.Next(100).ToString()
End Sub
End Class
Imports System
Imports System.Data
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports DevExpress.Web
Imports System.IO
Public Class FileCacheStrategy
Inherits RuntimeStorageStrategy
Public FileCacheFolderPath As String = "~/FileCacheFolder/"
Public Const CacheClearingInterval As Integer = 1 ' minutes
Public Const CacheExpirationTime As Integer = 10 ' minutes
Public Shared LastCacheClearing As Date = Date.MinValue
' GetResourceKey
' 1) the mime type is saved as a file's extension for further
' use within the GetResourceData method.
' 2) received key is used to generate the image url for further
' use within the StoreResourceData, GetResourceData
'
Public Overrides Function GetResourceKey(ByVal control As ASPxWebControlBase, ByVal content() As Byte, ByVal mimeType As String) As String
Dim fileExtension As String = ""
If mimeType.StartsWith("image/") Then
fileExtension = "." & mimeType.Substring("image/".Length)
End If
Return Guid.NewGuid().ToString() & fileExtension
End Function
Public Overrides Sub StoreResourceData(ByVal control As ASPxWebControlBase, ByVal key As String, ByVal data As BinaryStorageData)
ClearFileCacheIfNeeded(GetFolderPath())
Using stream As New FileStream(GetFilePath(key), FileMode.OpenOrCreate)
stream.Write(data.Content, 0, data.Content.Length)
End Using
End Sub
Public Overrides Function GetResourceData(ByVal key As String) As BinaryStorageData
Using stream As New FileStream(GetFilePath(key), FileMode.Open)
Dim content(stream.Length - 1) As Byte
stream.Read(content, 0, content.Length)
Dim mimeType As String = "image/" & key.Split("."c)(1)
Return New BinaryStorageData(content, mimeType)
End Using
Return Nothing
End Function
Private Shared Sub ClearFileCacheIfNeeded(ByVal physicalPath As String)
Dim now As Date = Date.Now
' cache clearing is launched after the specified time interval
If now.Subtract(LastCacheClearing) < TimeSpan.FromMinutes(CacheClearingInterval) Then
Return
End If
Dim fileNames() As String = Directory.GetFiles(physicalPath)
For Each fileName As String In fileNames
If now - File.GetCreationTime(fileName) > TimeSpan.FromMinutes(CacheExpirationTime) Then
Try
File.Delete(fileName)
Catch
End Try
End If
Next fileName
LastCacheClearing = now
End Sub
Private Function GetFilePath(ByVal key As String) As String
Return GetFolderPath() & key
End Function
Private Function GetFolderPath() As String
Return HttpContext.Current.Server.MapPath(FileCacheFolderPath)
End Function
End Class
' images are cached within the predefined public folder. The method returns an image url.
Public Class StaticImageStrategy
Inherits RuntimeStorageStrategy
Public StaticCacheFolderName As String = "StaticCacheFolder" ' public folder
Public Const FileExpirationTime As Integer = 60 ' minutes
Public Overrides Function GetResourceUrl(ByVal control As ASPxWebControlBase, ByVal content() As Byte, ByVal mimeType As String, ByVal contentDisposition As String) As String
Dim fileExtension As String = ""
Dim a = control.NamingContainer.BindingContainer
If mimeType.StartsWith("image/") Then
fileExtension = "." & mimeType.Substring("image/".Length)
End If
Dim fileName As String = control.Attributes("data-key") & fileExtension
Dim physicalPath As String = HttpContext.Current.Server.MapPath(String.Format("~/{0}/", StaticCacheFolderName)) & fileName
If File.Exists(physicalPath) AndAlso Date.Now - File.GetCreationTime(physicalPath) > TimeSpan.FromMinutes(FileExpirationTime) Then
File.Delete(physicalPath) ' an image is refreshed every hour
End If
If Not File.Exists(physicalPath) Then
Using stream As New FileStream(physicalPath, FileMode.OpenOrCreate)
stream.Write(content, 0, content.Length)
End Using
End If
Return String.Format("{0}/{1}", StaticCacheFolderName, fileName)
End Function
End Class
<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="CacheSample" %>
<%@ Register Assembly="DevExpress.Web.ASPxGauges.v17.1" Namespace="DevExpress.Web.ASPxGauges.Gauges" TagPrefix="dx" %>
<%@ Register assembly="DevExpress.Web.ASPxGauges.v17.1" namespace="DevExpress.Web.ASPxGauges" tagprefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxGauges.v17.1" Namespace="DevExpress.Web.ASPxGauges.Gauges.Linear" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxGauges.v17.1" Namespace="DevExpress.Web.ASPxGauges.Gauges.Circular" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxGauges.v17.1" Namespace="DevExpress.Web.ASPxGauges.Gauges.State" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxGauges.v17.1" Namespace="DevExpress.Web.ASPxGauges.Gauges.Digital" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v17.1" Namespace="DevExpress.Web" TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
FileCacheStrategy demonstration:
<br />
<dx:ASPxGaugeControl ID="gaugeControl1" runat="server" BackColor="White" Height="260px"
Value="20" Width="260px">
<Gauges>
<dx:CircularGauge Bounds="0, 0, 260, 260" Name="cGauge1">
<BackgroundLayers>
<dx:ArcScaleBackgroundLayerComponent Name="bg1" ScaleCenterPos="0.5, 0.61" ScaleID="scale1"
Size="250, 205" ZOrder="1000" ShapeType="CircularThreeFourth_Style7"></dx:ArcScaleBackgroundLayerComponent>
</BackgroundLayers>
<Needles>
<dx:ArcScaleNeedleComponent EndOffset="-25" StartOffset="-21" ScaleID="scale1" Name="needle1"
ZOrder="-50" ShapeType="CircularFull_Style7"></dx:ArcScaleNeedleComponent>
</Needles>
<EffectLayers>
<dx:ArcScaleEffectLayerComponent Name="effect1"
Shader="<ShaderObject Type="Opacity" Data="Opacity[0.75]"/>"
ScaleID="scale1" Size="230, 110" ZOrder="-1000" ShapeType="CircularThreeFourth_Style7">
</dx:ArcScaleEffectLayerComponent>
</EffectLayers>
<Scales>
<dx:ArcScaleComponent Name="scale1" MaxValue="100" Value="20" MinorTickmark-ShapeType="Circular_Style7_1"
Center="125, 140" EndAngle="30" MajorTickCount="9" MinorTickCount="4" MajorTickmark-TextOffset="22"
MajorTickmark-TextOrientation="LeftToRight" MajorTickmark-FormatString="{0:F0}"
MajorTickmark-ShapeType="Circular_Style7_2" StartAngle="-210" RadiusX="83" RadiusY="83">
</dx:ArcScaleComponent>
</Scales>
</dx:CircularGauge>
</Gauges>
</dx:ASPxGaugeControl>
<br />
<dx:ASPxButton ID="ASPxButton1" runat="server" OnClick="ASPxButton1_Click" Text="Generate random value">
</dx:ASPxButton>
<br />
<br />
StaticImageStrategy demonstration:
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" OnHtmlDataCellPrepared="ASPxGridView1_HtmlDataCellPrepared"
DataSourceID="AccessDataSource1" KeyFieldName="CategoryID">
<Columns>
<dx:GridViewDataTextColumn FieldName="CategoryID" ReadOnly="True" VisibleIndex="0">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="CategoryName" VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="Description" VisibleIndex="2">
</dx:GridViewDataTextColumn>
<dx:GridViewDataBinaryImageColumn FieldName="Picture" Name="Picture" VisibleIndex="3">
</dx:GridViewDataBinaryImageColumn>
</Columns>
</dx:ASPxGridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/nwind.mdb"
SelectCommand="SELECT * FROM [Categories]"></asp:AccessDataSource>
</form>
</body>
</html>