在本人的这篇文章《Xmemcached集群与SpringBoot整合》基础上,进行XMemcached与SpringBoot实际案例的结合。
有以下这张表,将这张表的增删改查操作都添加到XMemcached中进行:
将id用逗号拼接保存在indexUuids,每个id对应的对象用json的方式单独存储。
package com.czhappy.huans.xmemcached;
import com.czhappy.huans.entity.Area;
import java.util.List;
public interface AreaCacheDAO {
void addArea(Area area) throws Exception;
void updateArea(Area area) throws Exception;
void delArea(Integer uuid) throws Exception;
List<Area> getAreas() throws Exception;
Area getAreaById(Integer uuid) throws Exception;
}
package com.czhappy.huans.xmemcached;
import com.alibaba.fastjson.JSON;
import com.czhappy.huans.Utils.ToolUtil;
import com.czhappy.huans.entity.Area;
import com.czhappy.huans.service.AreaService;
import net.rubyeye.xmemcached.MemcachedClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@Service
public class AreaCacheDAOImpl implements AreaCacheDAO {
private static final String INDEX_NAME="indexUuids";
@Autowired
private MemcachedClient memcachedClient;
@Autowired
private AreaService areaService;
/*
思路一:
1、indexUuids : 1,2,3,4,5,6,7
2、
area:1 -> json
area:2 -> json
area:3 -> json
area:4 -> json
area:6 -> json
思路二:
list<Area> -> json -> key: indexObjects
*/
@Override
public void addArea(Area area) throws Exception {
// 先修改数据库
boolean isSuccess = areaService.addArea(area);
if(isSuccess){
// 转换为JSON实现
String areaStr = JSON.toJSONString(area);
// 如果成功,则修改缓存
// 判断indexUuids存不存在 "" " "
String uuids = memcachedClient.get(INDEX_NAME);
if(uuids!=null && uuids.trim().length()>0){
// 如果存在,修改indexUuids
memcachedClient.append(INDEX_NAME,","+area.getAreaId());
memcachedClient.set(""+area.getAreaId(),0,areaStr);
}else{
// 如果不存在, 初始化一个indexUuids -> tblhouse.uuid
memcachedClient.set(INDEX_NAME,0,""+area.getAreaId());
memcachedClient.set(""+area.getAreaId(),0,areaStr);
}
}else{
// TODO -> 可以选择返回或抛例外
}
}
@Override
public void updateArea(Area area) throws Exception {
// 先修改数据库
boolean isSuccess = areaService.modifyArea(area);
if(isSuccess){
// 转换为JSON实现
String areaStr = JSON.toJSONString(area);
// 如果成功,则修改缓存
// 判断indexUuids存不存在 "" " "
String uuids = memcachedClient.get(INDEX_NAME);
if(uuids!=null && uuids.trim().length()>0){
memcachedClient.replace(""+area.getAreaId(),0,areaStr);
}else{
// 如果不存在, 初始化一个indexUuids -> tblhouse.uuid
memcachedClient.set(INDEX_NAME,0,""+area.getAreaId());
memcachedClient.replace(""+area.getAreaId(),0,areaStr);
}
}else{
// TODO -> 可以选择返回或抛例外
}
}
@Override
public void delArea(Integer uuid) throws Exception {
boolean isSuccess = areaService.deleteArea(uuid);
String uuidStr = ""+uuid;
if(isSuccess){
// 修改indexUuids 101,3,4,5,101,202,2,404
String uuids = memcachedClient.get(INDEX_NAME);
if(!ToolUtil.isEmpty(uuids)) {
// 将字符串转换为集合,循环寻找匹配值,然后删除,将结果放入缓存
// 将uuids作为字符串处理 uuid=,2, replace ,
// 如果为首位 -> uuid, -> subString
if(uuids.startsWith(uuid+",")){
uuids = uuids.substring(uuidStr.length()+1);
}else if(uuids.endsWith(","+uuid)){
// 如果是结尾 -> ,uuid -> sub
int endIndex = uuids.length() - (uuidStr.length()+1);
uuids = uuids.substring(0,endIndex);
}else{
// 如果是中间 -> ,uuid, -> replace -> ,
uuids = uuids.replace(","+uuid+",",",");
}
// 更新索引列表
memcachedClient.set(INDEX_NAME,0,uuids);
}
// 删除对应的数据
memcachedClient.delete(uuidStr);
}else{
// TODO -> 可以选择返回或抛例外
}
}
@Override
public List<Area> getAreas() throws Exception {
// 101,1,2,3,4,5,7,202,404
List<String> keys = new ArrayList<>();
List<Area> areas = new ArrayList<>();
String uuids = memcachedClient.get(INDEX_NAME);
// 组织所有key集合
keys = Arrays.asList(uuids.split(","));
// 这是一个反例
// Map<String,Object> maps = memcachedClient.get(keys);
// for(Object obj : maps.values()){
// areas.add((Area) obj);
// }
// 正常的操作形式
Map<String,String> maps = memcachedClient.get(keys);
for(String areaJson : maps.values()){
Area area = JSON.parseObject(areaJson,Area.class);
areas.add(area);
}
return areas;
}
@Override
public Area getAreaById(Integer uuid) throws Exception {
String areaJSON = memcachedClient.get(""+uuid);
Area area = JSON.parseObject(areaJSON,Area.class);
return area;
}
}
测试结果: