新建一個Action:RClasAction,實現向Redis添加課程數據,並同步到MySQL。
package com.school.action; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import com.opensymphony.xwork2.ActionSupport; import com.school.entity.Clas; import com.school.redisclient.RedisService; import com.school.redisclient.RedisTool; import com.school.service.ClasService; @SuppressWarnings("serial") public class RClasAction extends ActionSupport { @Autowired private ClasService clasService; RedisService rs = RedisTool.getRedisService(); List<Clas> claslist = new ArrayList<Clas>(); private Clas clas; public Clas getClas() { return clas; } public void setClas(Clas Clas) { this.clas = Clas; } public String execute(){ saveClas(clas); return SUCCESS; } @SuppressWarnings({ "rawtypes", "unchecked" }) private void saveClas(Clas c){ List<String> ids = rs.getList("clas:id"); // clas:id int num = ids.size(); int id = Integer.parseInt(ids.get(num-1)) + 1; rs.rightPushList("clas:id", String.valueOf(id)); // clas:count int count = Integer.parseInt(rs.get("clas:count")); rs.set("clas:count", String.valueOf(count+1), -1); // 增加 HashMap hashmap = new HashMap(); hashmap.put("ID", String.valueOf(id)); hashmap.put("NAME", clas.getName()); hashmap.put("COMMENT", clas.getComment()); rs.addHash("clas:" + id, hashmap); // 同步到MySQL clasService.saveClas(clas); } }
clas:id是一個List類型的Key-Value,記錄了所有的課程ID,取出最後一個ID,再+1,作為增加的課程的ID,同時clas:count的值也要+1。使用addHash()方法向Redis添加了一個Hash類型的Key-Value(也就是一門課程):
@SuppressWarnings({ "unchecked", "rawtypes" }) public synchronized void addHash(K key, HashMap map){ redisTemplate.opsForHash().putAll(key, map); }
同時將該門課程增加到MySQL。
新建一個Action:RClasDeleteAction,實現刪除Redis的課程數據,並同步到MySQL。
package com.school.action; import org.springframework.beans.factory.annotation.Autowired; import com.opensymphony.xwork2.ActionSupport; import com.school.redisclient.RedisService; import com.school.redisclient.RedisTool; import com.school.service.ClasService; @SuppressWarnings("serial") public class RClasDeleteAction extends ActionSupport { @Autowired private ClasService clasService; RedisService rs = RedisTool.getRedisService(); private int id; public int getId(){ return id; } public void setId(int id){ this.id=id; } public String execute(){ deleteClas(id); // 同步到MySQL clasService.deleteClas(id); return SUCCESS; } private void deleteClas(int id){ // 刪除 rs.del("clas:" + id); // clas:count int count = Integer.parseInt(rs.get("clas:count")); rs.set("clas:count", String.valueOf(count-1), -1); // clas:id rs.delListItem("clas:id", String.valueOf(id)); } }
直接刪除clas:id,再將clas:count的值-1,這兩步比較簡單,復雜的是從clas:id中刪除該課程的ID,使用了delListItem()方法來實現:
@Override public synchronized void delListItem(K key, V value){ redisTemplate.opsForList().remove(key, 1, value); }
redisTemplate.opsForList().remove()方法類似於LREM命令。最後在MySQL中也刪除相同的課程。
新建一個Action:RClasUpdateAction,實現刪除Redis的課程數據,並同步到MySQL。
package com.school.action; import java.util.HashMap; import org.springframework.beans.factory.annotation.Autowired; import com.opensymphony.xwork2.ActionSupport; import com.school.entity.Clas; import com.school.redisclient.RedisService; import com.school.redisclient.RedisTool; import com.school.service.ClasService; @SuppressWarnings("serial") public class RClasUpdateAction extends ActionSupport{ @Autowired private ClasService clasService; RedisService rs = RedisTool.getRedisService(); private Clas clas; public Clas getClas() { return clas; } public void setClas(Clas clas) { this.clas = clas; } @SuppressWarnings({ "unchecked", "rawtypes" }) public String execute(){ HashMap hashmap = new HashMap(); hashmap.put("ID", String.valueOf(clas.getId())); hashmap.put("NAME", clas.getName()); hashmap.put("COMMENT", clas.getComment()); rs.putHash("clas:" + clas.getId(), hashmap); // 同步到MySQL clasService.updateClas(clas); return SUCCESS; } }
使用了putHash()方法來更新:
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public synchronized void putHash(K key, HashMap map){ redisTemplate.boundHashOps(key).putAll(map); }
同時在MySQL做相同的更新操作。