SSH架構和Redis的整合(2),ssh架構redis整合

來源:互聯網
上載者:User

SSH架構和Redis的整合(2),ssh架構redis整合
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做相同的更新操作。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.