Integration of the SSH framework and Redis (2), integration of the ssh framework redis

Source: Internet
Author: User

Integration of the SSH framework and Redis (2), integration of the ssh framework redis
5. Implementation of the add function

Create an Action: RClasAction to add course data to Redis and synchronize it to 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) {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); // Add 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); // synchronize to MySQL clasService. saveClas (clas );}}

Clas: id is a List-type Key-Value. It records all the course IDs, extracts the last ID, and then + 1 as the ID of the added course. At the same time, clas: the value of count must be + 1. Add a Hash Key-Value (that is, a course) to Redis using the addHash () method ):

        @SuppressWarnings({ "unchecked", "rawtypes" })        public synchronized void addHash(K key, HashMap map){            redisTemplate.opsForHash().putAll(key, map);        }

At the same time, add this course to MySQL.

6. Implementation of the delete function

Create an Action: RClasDeleteAction to delete Redis course data and synchronize it to 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); // synchronize to MySQL clasService. deleteClas (id); return SUCCESS;} private void deleteClas (int id) {// Delete 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 ));}}

Directly Delete clas: id, and then the value of clas: count-1. The two steps are relatively simple. The complex is to delete the id of the course from clas: ID, the delListItem () method is used for implementation:

        @Override        public synchronized void delListItem(K key, V value){            redisTemplate.opsForList().remove(key, 1, value);        }

The redisTemplate. opsForList (). remove () method is similar to the LREM command. Finally, the same course is also deleted in MySQL.

7. Implementation of the modification function

Create an Action: RClasUpdateAction to delete Redis course data and synchronize it to 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); // synchronize to MySQL clasService. updateClas (clas); return SUCCESS ;}}

The putHash () method is used to update:

        @SuppressWarnings({ "rawtypes", "unchecked" })        @Override        public synchronized void putHash(K key, HashMap map){            redisTemplate.boundHashOps(key).putAll(map);        }

Perform the same update operation in MySQL.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.