分布式SessionState“伪”实现

通常意义上的分布式状态服务器,需要平衡负载,考虑服务处理调度,以下方法摒弃其精髓,按随机算法分派状态服务。

实现起来较为简单。


1、Web.config

<sessionState mode="StateServer"  timeout="20" partitionResolverType="Pay.HttpModule.PartitionResolver" />


2、实现

    /// <summary>
    /// 分布式SessionState
    /// </summary>
    public class PartitionResolver : System.Web.IPartitionResolver
    {
        private String[] partitions;

        public void Initialize()
        {
            // create the partition connection string table
            partitions = PayConfig.SessionServers.Split(new char[] { ',' });
        }

        public String ResolvePartition(Object key)
        {
            if (String.IsNullOrEmpty(PayConfig.SessionServers))
            {
                return "";
            }
            String sid = key as string;
            // hash the incoming session ID into   
            // one of the available partitions 
            int partitionID = Math.Abs(sid.GetHashCode()) % partitions.Length;
            return "tcpip=" + partitions[partitionID];
        }
    }

你可能感兴趣的:(分布式SessionState“伪”实现)