forked from jimingmin/frame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_lock.cpp
177 lines (146 loc) · 4.03 KB
/
redis_lock.cpp
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
/*
* redis_lock.cpp
*
* Created on: 2015年4月2日
* Author: jimm
*/
#include "redis_lock.h"
#include "common/common_datetime.h"
#include "redissession_bank.h"
void RedisLock::SetSessionIndex(uint32_t nSessionIndex)
{
m_nSessionIndex = nSessionIndex;
}
uint32_t RedisLock::GetSessionIndex()
{
return m_nSessionIndex;
}
uint8_t *RedisLock::GetSessionData()
{
CRedisSessionBank *pRedisSessionBank = (CRedisSessionBank *)g_Frame.GetBank(BANK_REDIS_SESSION);
RedisSession *pRedisSession = pRedisSessionBank->GetSession(m_nSessionIndex);
return pRedisSession->GetSessionData();
}
void RedisLock::SetRedisChannel(CRedisChannel *pRedisChannel)
{
m_pRedisChannel = pRedisChannel;
}
CRedisChannel *RedisLock::GetRedisChannel()
{
return m_pRedisChannel;
}
void RedisLock::SetHandlerObj(CBaseObject *pObj)
{
m_pHandlerObj = pObj;
}
CBaseObject *RedisLock::GetHandlerObj()
{
return m_pHandlerObj;
}
void RedisLock::SetLockResultCallback(LockResult result)
{
m_fcLockResult = result;
}
LockResult RedisLock::GetLockResultCallback()
{
return m_fcLockResult;
}
int32_t RedisLock::Lock(char *szKey)
{
m_strLockName = string(szKey);
CRedisSessionBank *pRedisSessionBank = (CRedisSessionBank *)g_Frame.GetBank(BANK_REDIS_SESSION);
RedisSession *pRedisSession = pRedisSessionBank->GetSession(m_nSessionIndex);
return m_pRedisChannel->SetNX(pRedisSession, szKey, CDateTime::CurrentDateTime().Seconds());
}
int32_t RedisLock::Unlock()
{
int64_t nCurMilliTime = CTimeValue::CurrentTime().Seconds();
if(nCurMilliTime > m_nLockExpireTime)
{
return 0;
}
return m_pRedisChannel->Del(NULL, m_strLockName.c_str());
}
int32_t RedisLock::OnSessionSetNX(int32_t nResult, void *pReply, void *pSession)
{
redisReply *pRedisReply = (redisReply *)pReply;
RedisSession *pRedisSession = (RedisSession *)pSession;
int32_t nLockResult = enmRedisLock_Success;
if(pRedisReply->type == REDIS_REPLY_ERROR)
{
nLockResult = enmRedisLock_RedisError;
}
else if(pRedisReply->type == REDIS_REPLY_INTEGER)
{
if(pRedisReply->integer != 0)
{
pRedisSession->SetHandleRedisReply(static_cast<RedisReply>(&RedisLock::OnSessionGet));
return m_pRedisChannel->Get(pRedisSession, m_strLockName.c_str());
}
}
else if(pRedisReply->type == REDIS_REPLY_NIL)
{
Lock((char *)m_strLockName.c_str());
}
else
{
ASSERT(0);
}
(m_pHandlerObj->*m_fcLockResult)(nLockResult, this);
return 0;
}
int32_t RedisLock::OnSessionGet(int32_t nResult, void *pReply, void *pSession)
{
redisReply *pRedisReply = (redisReply *)pReply;
RedisSession *pRedisSession = (RedisSession *)pSession;
int32_t nLockResult = enmRedisLock_Success;
if(pRedisReply->type == REDIS_REPLY_ERROR)
{
nLockResult = enmRedisLock_RedisError;
}
else if(pRedisReply->type == REDIS_REPLY_STRING)
{
int64_t nCurMilliTime = CTimeValue::CurrentTime().Milliseconds();
int64_t nLockMilliTime = atoi64(pRedisReply->str);
if(nCurMilliTime > nLockMilliTime + (5 * MS_PER_SECOND))
{
m_nLockExpireTime = nCurMilliTime + (30 * MS_PER_SECOND);
pRedisSession->SetHandleRedisReply(static_cast<RedisReply>(&RedisLock::OnSessionGetSet));
return m_pRedisChannel->GetSet(pRedisSession, m_strLockName.c_str(), m_nLockExpireTime);
}
}
else
{
ASSERT(0);
}
(m_pHandlerObj->*m_fcLockResult)(nLockResult, this);
return 0;
}
int32_t RedisLock::OnSessionGetSet(int32_t nResult, void *pReply, void *pSession)
{
redisReply *pRedisReply = (redisReply *)pReply;
RedisSession *pRedisSession = (RedisSession *)pSession;
int32_t nLockResult = enmRedisLock_Success;
if(pRedisReply->type == REDIS_REPLY_ERROR)
{
nLockResult = enmRedisLock_RedisError;
}
else if(pRedisReply->type == REDIS_REPLY_STRING)
{
int64_t nGetMilliTime = atoi64(pRedisReply->str);
if(nGetMilliTime != m_nLockExpireTime)
{
nLockResult = enmRedisLock_Fail;
}
}
(m_pHandlerObj->*m_fcLockResult)(nLockResult, this);
return 0;
}
//int32_t RedisLock::OnRedisSessionTimeout(void *pTimerData)
//{
// RedisSession *pRedisSession = (RedisSession *)pTimerData;
//
// (m_pHandlerObj->*m_fcLockResult)(enmRedisLock_Timeout, this);
//
// return 0;
//}