@Component
public class HuaweiSmartLifeUserCacheData extends IValueRedisService<String> {
@Override
protected String getRedisKey() {
return CacheKey.HUAWEI_SMART_LIFE_USER_KEY;
}
}
@Component
public class HuaweiSmartLifeUserCacheUtils {
private static final Logger logger = LoggerFactory.getLogger(HuaweiSmartLifeUserCacheUtils.class);
@Autowired
private HuaweiSmartLifeUserCacheData huaweiSmartLifeUserCacheData;
public Boolean existsKey(String uid) {
String key = getKey(uid);
String uids = huaweiSmartLifeUserCacheData.get(key);
if (StringUtils.isEmpty(uids)) {
return false;
}
return uids.contains(uid);
}
public String setKey(String uid) {
String key = getKey(uid);
String uids = huaweiSmartLifeUserCacheData.get(key);
logger.info("uids:" + uids);
if (StringUtils.isEmpty(uids)) {
uids = "";
uids += uid;
logger.info("1:" + uids);
huaweiSmartLifeUserCacheData.put(key, uids);
}
if (!uids.contains(uid)) {
uids += "," + uid;
logger.info("2:" + uids);
huaweiSmartLifeUserCacheData.put(key, uids);
}
return key;
}
public List<String> getByKey(String uid) {
String key = getKey(uid);
String uids = huaweiSmartLifeUserCacheData.get(key);
List<String> uidList;
if (StringUtils.isEmpty(uids)) {
uidList = new ArrayList<>();
} else {
uidList = Arrays.asList(uids.split(","));
}
return uidList;
}
public String removeByKey(String uid) {
String key = getKey(uid);
huaweiSmartLifeUserCacheData.remove(key);
return key;
}
public String getKey(String str) {
String prefix = "_group";
return prefix + getHashKey(str);
}
public static int getHashKey(String key) {
return (hash(key) & Integer.MAX_VALUE) % 5;
}
public static int hash(String key) {
int hashValue = 0;
if (StringUtils.isNotEmpty(key)) {
key = key.toLowerCase();
}
for (int i = 0; i < key.length(); i++) {
int letter = key.charAt(i) - 'a' + 1;
hashValue += letter;
}
return hashValue & Integer.MAX_VALUE;
}
}
@RestController
@RequestMapping("/v1")
public class UserCacheController {
private static final Logger logger = LoggerFactory.getLogger(UserCacheController.class);
@Autowired
private IHuaweiSmartLifeUserInfoService huaweiSmartLifeUserService;
@Autowired
private HuaweiSmartLifeUserCacheUtils userCacheUtils;
@Autowired
private HuaweiSmartLifeUserCacheData huaweiSmartLifeUserCacheData;
@GetMapping("/user/initCache")
public JSONObject initCache() {
JSONObject jsonObject = new JSONObject();
List<HuaweiSmartLifeUserInfo> userInfoList = HuaweiSmartLifeUserInfoService.selectAll();
if (userInfoList == null || userInfoList.size() == 0) {
return jsonObject;
}
userInfoList = userInfoList.stream().limit(50).collect(Collectors.toList());
logger.info("绑定用户数:" + userInfoList.size());
String key;
for (HuaweiSmartLifeUserInfo userInfo : userInfoList) {
key = userCacheUtils.setKey(userInfo.getThirdPartyUserId());
if (jsonObject.containsKey(key)) {
int temp = jsonObject.getInteger(key);
jsonObject.put(key, temp + 1);
} else {
jsonObject.put(key, 1);
}
}
return jsonObject;
}
@GetMapping("/user/getAllCache")
public JSONObject getAllCache() {
Set<String> keys = huaweiSmartLifeUserCacheData.keys();
logger.info("keys:" + keys.toString());
JSONObject jsonObject = new JSONObject();
String cacheDevices;
List<String> deviceList;
for (String key : keys) {
logger.info("key:" + key.replace("huawei_smart_life_user", ""));
cacheDevices = huaweiSmartLifeUserCacheData.get(key.replace("huawei_smart_life_user", ""));
if (StringUtils.isNotEmpty(cacheDevices)) {
deviceList = Arrays.asList(cacheDevices.split(","));
jsonObject.put(key, deviceList);
}
}
return jsonObject;
}
@GetMapping("/user/removeAllCache")
public void removeAllCache() {
Set<String> keys = huaweiSmartLifeUserCacheData.keys();
for (String key : keys) {
key = key.replace("huawei_smart_life_user", "");
logger.info("key:" + key.replace("huawei_smart_life_user", ""));
userCacheUtils.removeByKey(key);
}
}
@GetMapping("/user/setByKey/{uid}")
public JSONObject setByKey(@PathVariable(value = "uid") String uid) {
JSONObject jsonObject = new JSONObject();
logger.info("uid:" + uid);
String key = userCacheUtils.setKey(uid);
jsonObject.put("key", key);
return jsonObject;
}
@GetMapping("/user/getByKey/{uid}")
public Set<String> getByKey(@PathVariable(value = "uid") String uid) {
String key = userCacheUtils.getKey(uid);
logger.info("uid=" + uid + "key=" + key);
List<String> list = userCacheUtils.getByKey(key);
Set<String> set = new HashSet<>(list);
return set;
}
@GetMapping("/user/removeByKey/{uid}")
public void removeByKey(@PathVariable(value = "uid") String uid) {
String key = userCacheUtils.getKey(uid);
List<String> list = userCacheUtils.getByKey(key);
logger.info("remove before:" + JSONArray.toJSONString(list));
if (list.contains(uid)) {
list = list.stream().filter(item -> !item.equals(uid)).collect(Collectors.toList());
}
logger.info("remove end:" + JSONArray.toJSONString(list));
if (list.size() > 0) {
huaweiSmartLifeUserCacheData.put(key, String.join(",", list));
} else {
userCacheUtils.removeByKey(key);
}
}
}
补充
- 本示例中,主要是removeByKey()要着重看一下,该方法用于实现单个key删除
- HiLinkUserCacheUtils中的hash()方法是用于实现hash取模运算的关键一步