summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenId/AssociationMemoryStore.cs
blob: 43dde01d156c65007cc883091dfe7af2821ca5ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Web.Caching;
using DotNetOpenId;


namespace DotNetOpenId {

	internal class AssociationMemoryStore<TKey> : IAssociationStore<TKey> {
		Dictionary<TKey, Associations> serverAssocsTable = new Dictionary<TKey, Associations>();

		internal Associations GetServerAssocs(TKey distinguishingFactor) {
			lock (this) {

				if (!serverAssocsTable.ContainsKey(distinguishingFactor)) {
					serverAssocsTable.Add(distinguishingFactor, new Associations());
				}

				return serverAssocsTable[distinguishingFactor];
			}
		}

		public void StoreAssociation(TKey distinguishingFactor, Association assoc) {
			lock (this) {
				if (!serverAssocsTable.ContainsKey(distinguishingFactor))
					serverAssocsTable.Add(distinguishingFactor, new Associations());

				Associations server_assocs = serverAssocsTable[distinguishingFactor];

				server_assocs.Set(assoc);
			}
		}

		public Association GetAssociation(TKey distinguishingFactor) {
			lock (this) {
				return GetServerAssocs(distinguishingFactor).Best;
			}
		}

		public Association GetAssociation(TKey distinguishingFactor, string handle) {
			lock (this) {
				return GetServerAssocs(distinguishingFactor).Get(handle);
			}
		}

		public bool RemoveAssociation(TKey distinguishingFactor, string handle) {
			lock (this) {
				return GetServerAssocs(distinguishingFactor).Remove(handle);
			}
		}

		/// <summary>
		/// Clears all expired associations from the store.
		/// </summary>
		public void ClearExpiredAssociations() {
			lock (this) {
				foreach (Associations assocs in serverAssocsTable.Values) {
					assocs.ClearExpired();
				}
			}
		}
	}
}