发表于: 2018-05-19 23:14:15

2 1363


今天完成的事情

  • 完成Spring MVC memcachede 融合

整合

  1. memcachede只缓存数据, 所以我们只需要在service 层 的实现中添加对应的缓存即可.
  • 原接口添加缓存
  • @Override
    public UserCustom findUserById(Integer id) throws Exception {
       // 查找缓存
       Object object = MemcacheUtils.get("user" + id);
       // 当存在缓存时直接返回缓存数据
       if (object != null) {
           return (UserCustom) object;
       }
       UserCustom userCustom = userDao.findUserById(id);
       // 当缓存为空时 添加 memcached 缓存
       MemcacheUtils.set("user" + id, userCustom);
       return userCustom;
    }

    @Override
    public int insertUser(User user) throws Exception {
       //插入成功后返回的值存入了userid
       userDao.insertUser(user);
       // 写入缓存 这里使用add key(id)存在时, 不写入缓存
       MemcacheUtils.add("user" + user.getId(), user);
       //所以返回userid
       return user.getId();
    }

    @Override
    public boolean updateUser(UserCustom userCustom, Integer id) throws Exception {
       userCustom.setId(id);
       // 写入缓存 这里使用replace, key(id)不存在时, 不写入缓存
       MemcacheUtils.replace("user" + id, userCustom);
       return userDao.updateUser(userCustom);
    }

    @Override
    public boolean deleteUser(Integer i) throws Exception {
       // 删除缓存
       MemcacheUtils.delete(String.valueOf(i));
       return userDao.deleteUser(i);
    }
  • 测试 查找数据, 第一次从数据库中查询并将值存入到缓存, 第二次直接从缓存获取 imageimageimage
  • 测试 更新数据, 再次获取, 直接从缓存中获取更新过的数据 imageimage
  1. 创建缓存api接口
  2. package com.jnshu.controller;

    import com.jnshu.model.UserCustom;
    import com.jnshu.service.UserService;
    import com.jnshu.tools.MemcacheUtils;
    import com.whalin.MemCached.MemCachedClient;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.util.StringUtils;
    import org.springframework.web.bind.annotation.*;


    /**
    * @program: taskTwo
    * @description: MemCache 缓存接口
    * @author: Mr.xweiba
    * @create: 2018-05-19 00:06
    **/

    @Controller
    @RequestMapping("/memcache")
    public class MemCacheController {
       @Autowired
       UserService userService;
       private static Logger logger = LoggerFactory.getLogger(MemCacheController.class);

       /**
       * @Description: 获取keyiduser缓存
       * @Param: [key]
       * @return: java.lang.Object
       * @Author: Mr.Wang
       * @Date: 2018/5/19
       */
       @RequestMapping(value = "/api/{id}", method = RequestMethod.GET)
       @ResponseBody
       public Object findByKey(@RequestBody @PathVariable("id") String key){
           if(StringUtils.isEmpty(key)){
               return "key must not be empty or null!";
           }
           return MemcacheUtils.get("user" + key);
       }

       /**
       * @Description: 缓存更新接口 当key不存在时取消更新
       * @Param: [key, userCustom] .
       * @return: boolean
       * @Author: Mr.Wang
       * @Date: 2018/5/19
       */
       @RequestMapping(value = "/api/{id}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
       @ResponseBody
       public boolean updateByKey(@PathVariable("id") String key, @RequestBody UserCustom userCustom){
           userCustom.setId(Integer.valueOf(key));
           if(StringUtils.isEmpty(key)){
               return false;
           }
           return MemcacheUtils.replace("user" + key, userCustom);
       }
       
       /**
       * @Description: 增加缓存数据 当键存在时取消存入
       * @Param: [key, userCustom] ,
       * @return: java.lang.Boolean
       * @Author: Mr.Wang
       * @Date: 2018/5/19
       */
       @RequestMapping(value = "/api/{id}", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
       @ResponseBody
       public Boolean insert(@PathVariable("id") String key, @RequestBody UserCustom userCustom){
           System.out.println(userCustom.toString());
           userCustom.setId(Integer.valueOf(key));
           if(StringUtils.isEmpty(key)){
               return false;
           }
           return MemcacheUtils.add("user" + key, userCustom);
       }
       
       /**
       * @Description: 删除指定key
       * @Param: [key]
       * @return: java.lang.Boolean
       * @Author: Mr.Wang
       * @Date: 2018/5/19
       */
       @RequestMapping(value = "/api/{id}", method = RequestMethod.DELETE)
       @ResponseBody
       public Boolean deleteByKey(@PathVariable("id") String key){
           if(StringUtils.isEmpty(key)){
               return false;
           }
           return MemcacheUtils.delete("user" + key);
       }
       /**
        * @Description: 清除缓存中的所有键值对
        * @Param: []
        * @return: boolean
        * @Author: Mr.Wang
        * @Date: 2018/5/19
        */
       @RequestMapping(value = "/api/all", method = RequestMethod.DELETE)
       @ResponseBody
       public boolean flashAll() throws Exception {
           return MemcacheUtils.flashAll();
       }
    }
  • 测试

错误

  1. 中文json数据插入后乱码
  • 接收方法添加接收字符类型 produces = "application/json; charset=utf-8"
  • @RequestMapping(value = "/api/{id}", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
  1. 存储对象时, 报错: exception thrown while writing bytes to server on set
  • 原因: memcachede接收的对象,必须序列化, 将存储的实体类实现Serializable接口即可
  • public class User implements Serializable {
  1. 将对象集合存入缓存, 取值时为空, 应该时集合序列化的问题, 还未解决

明日计划

  • 解决错误3
  • Nginx 负载测试



返回列表 返回列表
评论

    分享到