1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| 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.*;
@Controller @RequestMapping("/memcache") public class MemCacheController { @Autowired UserService userService; private static Logger logger = LoggerFactory.getLogger(MemCacheController.class);
@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); }
@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); }
@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); }
@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); }
@RequestMapping(value = "/api/all", method = RequestMethod.DELETE) @ResponseBody public boolean flashAll() throws Exception { return MemcacheUtils.flashAll(); } }
|