commit | author | age
|
a18bfa
|
1 |
package cn.autoform.web.controller.resumable.redis.impl; |
Z |
2 |
|
|
3 |
import cn.autoform.fw.exception.IEMRuntimeException; |
|
4 |
import cn.autoform.web.controller.resumable.redis.CacheTool; |
|
5 |
import cn.autoform.web.controller.resumable.redis.FormLock; |
|
6 |
import cn.autoform.web.controller.resumable.redis.lock.Lock; |
|
7 |
import cn.autoform.web.controller.resumable.redis.lock.LocksEntity; |
|
8 |
import lombok.Setter; |
|
9 |
import org.springframework.beans.factory.annotation.Value; |
|
10 |
import org.springframework.stereotype.Service; |
|
11 |
|
|
12 |
import javax.annotation.Resource; |
|
13 |
import java.io.Serializable; |
|
14 |
|
|
15 |
/** |
|
16 |
* 表单锁实现类 |
|
17 |
* @author wangyuxin |
|
18 |
* |
|
19 |
*/ |
|
20 |
@Service |
|
21 |
public class FormLockImpl implements FormLock { |
|
22 |
|
|
23 |
@Resource |
|
24 |
@Setter |
|
25 |
private CacheTool<Serializable> jedisCluster; |
|
26 |
|
|
27 |
@Value("${form.lock.time}") |
|
28 |
@Setter |
|
29 |
private long lockTime; |
|
30 |
|
|
31 |
/** |
|
32 |
* 表单锁的缓存key |
|
33 |
*/ |
|
34 |
public static final String LOCK_FORMAT = "form_%s_%d_%d_%d"; |
|
35 |
|
|
36 |
/* |
|
37 |
* 获取表单锁 |
|
38 |
*/ |
|
39 |
@Override |
|
40 |
public String getLock(String formId, Integer dataRowNum, Integer tenantId, Integer companyId) { |
|
41 |
String cacheKey; |
|
42 |
LocksEntity locks = jedisCluster.get(cacheKey = getKey(formId, dataRowNum, tenantId, companyId), LocksEntity.class); |
|
43 |
if(locks == null) { |
|
44 |
locks = new LocksEntity(); |
|
45 |
} |
|
46 |
Lock lock = locks.addLock(); |
|
47 |
jedisCluster.add(cacheKey, locks); |
|
48 |
return lock.getLockInfo(); |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
public String getKey(String formId, Integer dataRowNum, Integer tenantId, Integer companyId, String lockInfo) { |
|
53 |
System.out.println("时间是多少:"+lockTime); |
|
54 |
String cacheKey; |
|
55 |
LocksEntity locks = jedisCluster.get(cacheKey = getKey(formId, dataRowNum, tenantId, companyId), LocksEntity.class); |
|
56 |
if(locks == null) { |
|
57 |
throw new IEMRuntimeException("FORM_UPDATE"); |
|
58 |
} |
|
59 |
Lock lock = locks.addKey(lockInfo, System.currentTimeMillis() + lockTime); |
|
60 |
jedisCluster.add(cacheKey, locks); |
|
61 |
return lock.getKey().getKeyInfo(); |
|
62 |
} |
|
63 |
|
|
64 |
@Override |
|
65 |
public void unLock(String formId, Integer dataRowNum, Integer tenantId, Integer companyId, String lock, |
|
66 |
String key) { |
|
67 |
String cacheKey; |
|
68 |
LocksEntity locks = jedisCluster.get(cacheKey = getKey(formId, dataRowNum, tenantId, companyId), LocksEntity.class); |
|
69 |
// System.out.println(JSONTool.toJson(locks)); |
|
70 |
if(locks == null |
|
71 |
|| locks.getLock() == null |
|
72 |
|| !locks.getLock().getLockInfo().equals(lock) |
|
73 |
|| !locks.getLock().getKey().getKeyInfo().equals(key)) { |
|
74 |
throw new IEMRuntimeException("FORM_UPDATE"); |
|
75 |
} |
|
76 |
jedisCluster.delete(cacheKey); |
|
77 |
} |
|
78 |
|
|
79 |
public String getKey(String formId, Integer dataRowNum, Integer tenantId, Integer companyId) { |
|
80 |
return String.format(LOCK_FORMAT, formId, dataRowNum, tenantId, companyId); |
|
81 |
} |
|
82 |
|
|
83 |
@Override |
|
84 |
public Integer getLockSize(String formId, Integer dataRowNum, Integer tenantId, Integer companyId) { |
|
85 |
LocksEntity locks = jedisCluster.get(getKey(formId, dataRowNum, tenantId, companyId), LocksEntity.class); |
|
86 |
if(locks == null) { |
|
87 |
return 0; |
|
88 |
} |
|
89 |
return locks.getLocks().size(); |
|
90 |
} |
|
91 |
|
|
92 |
} |