aspnetcore/src/Microsoft.AspNet.Http/Collections/SessionCollection.cs

73 lines
1.7 KiB
C#

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
namespace Microsoft.AspNet.Http.Collections
{
public class SessionCollection : ISessionCollection
{
private readonly ISession _session;
public SessionCollection(ISession session)
{
_session = session;
}
public byte[] this[string key]
{
get
{
byte[] value;
TryGetValue(key, out value);
return value;
}
set
{
if (value == null)
{
Remove(key);
}
else
{
Set(key, new ArraySegment<byte>(value));
}
}
}
public bool TryGetValue(string key, out byte[] value)
{
return _session.TryGetValue(key, out value);
}
public void Set(string key, ArraySegment<byte> value)
{
_session.Set(key, value);
}
public void Remove(string key)
{
_session.Remove(key);
}
public void Clear()
{
_session.Clear();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<KeyValuePair<string, byte[]>> GetEnumerator()
{
foreach (var key in _session.Keys)
{
yield return new KeyValuePair<string, byte[]>(key, this[key]);
}
}
}
}