/*++

   Copyright    (c)    1999    Microsoft Corporation

   Module Name :

        connstate.cool

   Abstract:

        Connection state helper
        
   Author:

        Bilal Alam (BAlam)      2-Aug-99

   Environment:

        COM+ - User Mode Managed Run Time
       
   Project:

        Web Server
--*/

using System;
using System.ASP;
using System.ASP.Cache;
using System.ASP.Util;

namespace System.IIS
{
    internal abstract class ConnectionStateItem
    {
        public void OnCacheItemRemoved( Object sender, CacheItemRemovedEvent e )
        {
            Dispose();
        }
        
        public abstract void Dispose();
    };
                
    internal class ConnectionState
    {
        private const int CONNECTION_STATE_LIFE = 1000 * 60;
        
        static public ConnectionStateItem GetState( HttpContext context,
                                                    string strMod )
        {
            CacheKey cacheKey = new CacheKey( strMod,
                                   context.Request.ConnectionID.ToString(),
                                   null );
               
            return (ConnectionStateItem) HttpRuntime.Cache.Get( cacheKey );
        } 
        
        static public void SetState( HttpContext context, 
                                     string strMod,
                                     ConnectionStateItem value )
        {
            CacheKey cacheKey = new CacheKey( strMod,
                                   context.Request.ConnectionID.ToString(),
                                   null );
            
            CacheItemOptions cacheOptions = new CacheItemOptions();
            
            cacheOptions.ExpiresInterval = CONNECTION_STATE_LIFE;
            
            cacheOptions.OnRemoveHandler = new CacheItemRemovedHandler( 
                                                value.OnCacheItemRemoved );

            HttpRuntime.Cache.Add( cacheKey, value, cacheOptions );
        }
        
        static public void RemoveState( HttpContext context,
                                        string strMod )
        {
            CacheKey cacheKey = new CacheKey( strMod,
                                   context.Request.ConnectionID.ToString(),
                                   null );
            
            HttpRuntime.Cache.Remove( cacheKey );
        }
    };
}