您的位置:首页 > 数据库 > Redis

SSH框架和Redis的整合(2)

2017-01-05 16:07 113 查看

5. 添加功能的实现

新建一个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。

6. 删除功能的实现

新建一个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中也删除相同的课程。

7. 修改功能的实现

新建一个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做相同的更新操作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: