Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / C#

Pfz.Caching - ViewIds instead of ViewStates

Rate me:
Please Sign up or sign in to vote.
4.85/5 (13 votes)
10 May 2010CPOL8 min read 43K   239   25  
Framework for caching data that includes the possibility to store ViewStates in files, reutilizing identical files as an way to avoid too much HD usage
using System;
using System.Collections.Specialized;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web;
using System.Web.SessionState;
using Pfz.Threading;

namespace Pfz.Caching
{
	/// <summary>
	/// Class that provides session management features using CacheDictionary class.
	/// </summary>
	public sealed class PfzSessionStateStoreProvider:
		SessionStateStoreProviderBase
	{
		private IPfzSessionStateStoreProvider fProvider;
		private static string fSessionIdPrefix;
		
		internal static string GetId(string id)
		{
			if (fSessionIdPrefix == null)
				return id;
			
			return fSessionIdPrefix + id;
		}
		
		/// <summary>
		/// Connects to the SessionStateServer.
		/// </summary>
		public override void Initialize(string name, NameValueCollection config)
		{
			base.Initialize(name, config);
			
			string host = config.Get("host");
			int port = int.Parse(config.Get("port"));
			
			fSessionIdPrefix = config.Get("group");
			if (fSessionIdPrefix != null)
				fSessionIdPrefix += ':';
			
			string fullHost = string.Format("tcp://{0}:{1}/IPfzSessionStateStoreProvider", host, port);
			fProvider = (IPfzSessionStateStoreProvider)Activator.GetObject(typeof(IPfzSessionStateStoreProvider), fullHost);
		}
	
		/// <summary>
		/// Does nothing.
		/// </summary>
		public override void Dispose()
		{
			fProvider = null;
		}

		/// <summary>
		/// Only initializes an empty dictionary.
		/// </summary>
		public override SessionStateStoreData CreateNewStoreData(HttpContext context, int timeout)
		{
			var items = new PfzSessionStateItemCollection();
			var staticObjects = SessionStateUtility.GetSessionStaticObjects(context);
			return new SessionStateStoreData(items, staticObjects, timeout);
		}

		/// <summary>
		/// Does nothing.
		/// </summary>
		public override void CreateUninitializedItem(HttpContext context, string id, int timeout)
		{
		}

		/// <summary>
		/// Does nothing.
		/// </summary>
		public override void EndRequest(HttpContext context)
		{
		}

		/// <summary>
		/// Loads a session from disk. The values are then loaded from disk or memory.
		/// </summary>
		public override SessionStateStoreData GetItem(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
		{
			actions = SessionStateActions.None;
			
			TimeSpan timeout;
			long longLockId;
			byte[] bytes = fProvider.GetItem(GetId(id), out longLockId, out lockAge, out timeout);
			
			locked = (longLockId != 0);
			if (locked)
				lockId = longLockId;
			else
				lockId = null;
			
			if (bytes == null)
				return null;
			
			PfzSessionStateItemCollection dictionary = null;
			BinaryFormatter binaryFormatter = new BinaryFormatter();
			
			try
			{
				AbortSafe.Using
				(
					() => new MemoryStream(bytes),
					(stream) => dictionary = (PfzSessionStateItemCollection)binaryFormatter.Deserialize(stream)
				);
			}
			catch(SerializationException)
			{
				return null;
			}
			
			var staticObjects = SessionStateUtility.GetSessionStaticObjects(context);
			return new SessionStateStoreData(dictionary, staticObjects, (int)timeout.TotalMinutes);
		}

		/// <summary>
		/// Loads a session from disk and renames (locks) it. The values are then 
		/// loaded from disk or memory.
		/// </summary>
		public override SessionStateStoreData GetItemExclusive(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
		{
			actions = SessionStateActions.None;
		
			TimeSpan timeout;
			long longLockId;
			
			byte[] bytes = fProvider.GetItemExclusive(GetId(id), out longLockId, out locked, out lockAge, out timeout);
			
			if (longLockId == 0)
				lockId = null;
			else
				lockId = longLockId;
			
			if (bytes == null && lockId != null)
				return null;
			
			PfzSessionStateItemCollection dictionary = null;
			
			if (bytes == null)
				dictionary = new PfzSessionStateItemCollection();
			else
			{
				BinaryFormatter binaryFormatter = new BinaryFormatter();
				
				try
				{
					AbortSafe.Using
					(
						() => new MemoryStream(bytes),
						(stream) => dictionary = (PfzSessionStateItemCollection)binaryFormatter.Deserialize(stream)
					);
				}
				catch(SerializationException)
				{
					return null;
				}
			}
			
			var staticObjects = SessionStateUtility.GetSessionStaticObjects(context);
			return new SessionStateStoreData(dictionary, staticObjects, (int)timeout.TotalMinutes);
		}

		/// <summary>
		/// Does nothing.
		/// </summary>
		public override void InitializeRequest(HttpContext context)
		{
		}

		/// <summary>
		/// Only executed on exceptions.
		/// </summary>
		public override void ReleaseItemExclusive(HttpContext context, string id, object lockId)
		{
			long longLockId = 0;
			if (lockId != null)
				longLockId = (long)lockId;
			
			fProvider.ReleaseItem(GetId(id), longLockId);
		}

		/// <summary>
		/// Delete the file from disk.
		/// </summary>
		public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item)
		{
			fProvider.RemoveItem(GetId(id), (long)lockId);
		}
		
		/// <summary>
		/// Does a keep alive to the session.
		/// </summary>
		/// <param name="context"></param>
		/// <param name="id"></param>
		public override void ResetItemTimeout(HttpContext context, string id)
		{
			fProvider.KeepAlive(GetId(id));
		}

		/// <summary>
		/// Saves the session to the disk.
		/// </summary>
		public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData data, object lockId, bool newItem)
		{
			byte[] bytes = null;

			BinaryFormatter binaryFormatter = new BinaryFormatter();
			AbortSafe.Using
			(
				() => new MemoryStream(),
				(stream) =>
				{
					binaryFormatter.Serialize(stream, data.Items);
					bytes = stream.ToArray();
				}
			);
			
			long longLockId = 0;
			if (lockId != null)
				longLockId = (long)lockId;
				
			fProvider.SetAndReleaseItem(GetId(id), longLockId, bytes);
		}

		/// <summary>
		/// Does nothing. The callback is not registered.
		/// </summary>
		public override bool SetItemExpireCallback(SessionStateItemExpireCallback expireCallback)
		{
			return false;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Microsoft
United States United States
I started to program computers when I was 11 years old, as a hobbyist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.

At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do their work easier, faster and with less errors.

Want more info or simply want to contact me?
Take a look at: http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com

Codeproject MVP 2012, 2015 & 2016
Microsoft MVP 2013-2014 (in October 2014 I started working at Microsoft, so I can't be a Microsoft MVP anymore).

Comments and Discussions