CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
PrincipalCache.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="PrincipalCache.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Provides a cache for a limited number of</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Security.Principal;
11
12namespace Csla.Security
13{
18 public static class PrincipalCache
19 {
20 private static List<IPrincipal> _cache = new List<IPrincipal>();
21
22 private static int _maxCacheSize;
23
27 public static int MaxCacheSize
28 {
29 get
30 {
31 if (_maxCacheSize == 0)
32 {
33 string tmp = Csla.Configuration.ConfigurationManager.AppSettings["CslaPrincipalCacheSize"];
34 if (string.IsNullOrEmpty(tmp))
35 _maxCacheSize = 10;
36 else
37 _maxCacheSize = Convert.ToInt32(tmp);
38 }
39 return _maxCacheSize;
40 }
41 internal set
42 {
43 _maxCacheSize = value;
44 }
45 }
46
55 public static IPrincipal GetPrincipal(string name)
56 {
57 lock (_cache)
58 {
59 foreach (IPrincipal item in _cache)
60 if (item.Identity.Name == name)
61 return item;
62 return null;
63 }
64 }
65
72 public static void AddPrincipal(IPrincipal principal)
73 {
74 lock (_cache)
75 {
76 if (!_cache.Contains(principal))
77 {
78 _cache.Add(principal);
79 if (_cache.Count > MaxCacheSize)
80 _cache.RemoveAt(0);
81 }
82 }
83 }
84
88 public static void Clear()
89 {
90 lock (_cache)
91 _cache.Clear();
92 }
93 }
94}