1
yangle
昨天 6d0bfe2160e06a4502dc3052c43fab9813341db3
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Threading;
using System.Collections.Generic;
 
 
namespace Top.Api.Security
{
 
    /// <summary>
    /// 计数器
    /// </summary>
    public class SecurityCounter : SecurityConstants
    {
        private static readonly object Lock = new object();
        private static readonly IDictionary<string, Counter> AppCounter = new Dictionary<string, Counter>();
        private static readonly IDictionary<string, IDictionary<string, Counter>> AppUserCounter = new Dictionary<string, IDictionary<string, Counter>>();
        private string appkey;
        private static readonly object InitLock = new object();
 
        public SecurityCounter(string appkey)
        {
            this.appkey = appkey;
            lock (InitLock)
            {
                Counter appCounter = GetAppCounter(appkey);
                if (appCounter == null)
                {
                    appCounter = new Counter();
                    AppCounter.Add(appkey, appCounter);
                }
 
                IDictionary<string, Counter> userCounter = GetUserCounter(appkey);
                if (userCounter == null)
                {
                    userCounter = new Dictionary<string, Counter>();
                    AppUserCounter.Add(appkey, userCounter);
                }
            }
        }
 
        public static Counter GetAppCounter(string appkey)
        {
            Counter appCounter = null;
            AppCounter.TryGetValue(appkey, out appCounter);
            return appCounter;
        }
 
        public static IDictionary<string, Counter> GetUserCounter(string appkey)
        {
            IDictionary<string, Counter> userCounter = null;
            AppUserCounter.TryGetValue(appkey, out userCounter);
            return userCounter;
        }
 
        public static void AddEncryptCount(string type, Counter counter)
        {
            if (counter == null)
            {
                return;
            }
 
            if (PHONE.Equals(type))
            {
                Interlocked.Increment(ref counter.EncryptPhoneNum);
            }
            else if (NICK.Equals(type))
            {
                Interlocked.Increment(ref counter.EncryptNickNum);
            }
            else if (RECEIVER_NAME.Equals(type))
            {
                Interlocked.Increment(ref counter.EncryptReceiverNameNum);
            }
            else if (SIMPLE.Equals(type))
            {
                Interlocked.Increment(ref counter.EncryptSimpleNum);
            }
            else if (SEARCH.Equals(type))
            {
                Interlocked.Increment(ref counter.EncryptSearchNum);
            }
        }
 
        public static void AddDecryptCount(string type, Counter counter)
        {
            if (counter == null)
            {
                return;
            }
 
            if (PHONE.Equals(type))
            {
                Interlocked.Increment(ref counter.DecryptPhoneNum);
            }
            else if (NICK.Equals(type))
            {
                Interlocked.Increment(ref counter.DecryptNickNum);
            }
            else if (RECEIVER_NAME.Equals(type))
            {
                Interlocked.Increment(ref counter.DecryptReceiverNameNum);
            }
            else if (SIMPLE.Equals(type))
            {
                Interlocked.Increment(ref counter.DecryptSimpleNum);
            }
            else if (SEARCH.Equals(type))
            {
                Interlocked.Increment(ref counter.DecryptSearchNum);
            }
        }
 
        public static void AddSearchCount(string type, Counter counter)
        {
            if (counter == null)
            {
                return;
            }
 
            if (PHONE.Equals(type))
            {
                Interlocked.Increment(ref counter.SearchPhoneNum);
            }
            else if (NICK.Equals(type))
            {
                Interlocked.Increment(ref counter.SearchNickNum);
            }
            else if (RECEIVER_NAME.Equals(type))
            {
                Interlocked.Increment(ref counter.SearchReceiverNameNum);
            }
            else if (SIMPLE.Equals(type))
            {
                Interlocked.Increment(ref counter.SearchSimpleNum);
            }
            else if (SEARCH.Equals(type))
            {
                Interlocked.Increment(ref counter.SearchSearchNum);
            }
        }
 
        public void AddEncryptCount(string type, string session)
        {
            AddEncryptCount(type, GetCounter(session));
        }
 
        public void AddDecryptCount(string type, string session)
        {
            AddDecryptCount(type, GetCounter(session));
        }
 
        public void AddSearchCount(string type, string session)
        {
            AddSearchCount(type, GetCounter(session));
        }
 
        public static void CleanUserCounter(string appkey)
        {
            IDictionary<string, Counter> userCounter = GetUserCounter(appkey);
            if (userCounter != null)
            {
                lock (Lock)
                {
                    userCounter.Clear();
                }
            }
        }
 
        public static IDictionary<string, Counter> CloneUserCounter(string appkey)
        {
            IDictionary<string, Counter> targetDictionary = new Dictionary<string, Counter>();
            IDictionary<string, Counter> userCounter = GetUserCounter(appkey);
            if (userCounter == null)
            {
                return targetDictionary;
            }
          
            lock (Lock)
            {
                foreach (KeyValuePair<string, Counter> currentPair in userCounter)
                {
                    targetDictionary.Add(currentPair.Key, currentPair.Value);
                }
            }
            return targetDictionary;
        }
 
 
        private Counter GetCounter(string session)
        {
            Counter counter = null;
            if (session == null)
            {
                counter = GetAppCounter(appkey);
            }
            else
            {
                IDictionary<string, Counter> userCounter = GetUserCounter(appkey);
                if (userCounter == null)
                {
                    return null;
                }
                lock (Lock)
                {
                    userCounter.TryGetValue(session, out counter);
                    if (counter == null)
                    {
                        counter = new Counter();
                        userCounter.Add(session, counter);
                    }
                }
            }
            return counter;
        }
    }
}