{"id":201266,"date":"2025-05-10T11:06:02","date_gmt":"2025-05-10T03:06:02","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201266\/"},"modified":"2025-05-10T11:06:02","modified_gmt":"2025-05-10T03:06:02","slug":"%e8%af%a6%e8%a7%a3ssh%e6%a1%86%e6%9e%b6%e5%92%8credis%e7%9a%84%e6%95%b4%e5%90%88","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201266\/","title":{"rendered":"\u8be6\u89e3SSH\u6846\u67b6\u548cRedis\u7684\u6574\u5408"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u8be6\u89e3SSH\u6846\u67b6\u548cRedis\u7684\u6574\u5408<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u8be6\u89e3SSH\u6846\u67b6\u548cRedis\u7684\u6574\u5408\u300b\uff0c\u8986\u76d6\u4e86\u6570\u636e\u5e93\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p>\u4e00\u4e2a\u5df2\u6709\u7684Struts+Spring+Hibernate\u9879\u76ee\uff0c\u4ee5\u524d\u4f7f\u7528MySQL\u6570\u636e\u5e93\uff0c\u73b0\u5728\u60f3\u628aRedis\u4e5f\u6574\u5408\u8fdb\u53bb\u3002<\/p>\n<p><span style=\"color: #ff0000\"><strong>1. \u76f8\u5173Jar\u6587\u4ef6<\/strong><\/span><\/p>\n<p>\u4e0b\u8f7d\u5e76\u5bfc\u5165\u4ee5\u4e0b3\u4e2aJar\u6587\u4ef6\uff1a<\/p>\n<p>commons-pool2-2.4.2.jar\u3001jedis-2.3.1.jar\u3001spring-data-redis-1.3.4.RELEASE.jar\u3002<\/p>\n<p><span style=\"color: #ff0000\"><strong>2. Redis\u914d\u7f6e\u6587\u4ef6<\/strong><\/span><\/p>\n<p>\u5728src\u6587\u4ef6\u5939\u4e0b\u9762\u65b0\u5efa\u4e00\u4e2aredis.properties\u6587\u4ef6\uff0c\u8bbe\u7f6e\u8fde\u63a5Redis\u7684\u4e00\u4e9b\u5c5e\u6027\u3002<\/p>\n<pre>\nredis.host=127.0.0.1 \nredis.port=6379 \nredis.default.db=1 \nredis.timeout=100000 \nredis.maxActive=300 \nredis.maxIdle=100 \nredis.maxWait=1000 \nredis.testOnBorrow=true <\/pre>\n<p>\u518d\u65b0\u5efa\u4e00\u4e2aredis.xml\u6587\u4ef6\u3002<\/p>\n<pre>\n \n\n   \n    \n    \n     \n     \n     \n     \n     \n      \n       \n        classpath:redis.properties\n       \n      \n     \n    \n    \n     \n     \n    \n    \n     \n     \n     \n     \n     \n     \n    \n    \n    \n     \n    \n    \n   <\/pre>\n<p><span style=\"color: #ff0000\"><strong>3. Redis\u7c7b<\/strong><\/span><\/p>\n<p>\u65b0\u5efa\u4e00\u4e2acom.school.redisclient\u5305\uff0c\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167254475863b101f65d006.png\" class=\"aligncenter\"><\/p>\n<p>\u63a5\u53e3IRedisService\uff1a<\/p>\n<pre>\npublic interface IRedisService\n   \n     {  \n public void set(K key, V value, long expiredTime); \n public V get(K key);\n public Object getHash(K key, String name);\n public void del(K key);   \n} \n\n\n   <\/pre>\n<p>\u62bd\u8c61\u7c7bAbstractRedisService\uff0c\u4e3b\u8981\u662f\u5bf9RedisTemplate\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<pre>\npublic abstract class AbstractRedisService\n   \n     implements IRedisService\n    \n      { @Autowired private RedisTemplate\n     \n       redisTemplate; public RedisTemplate\n      \n        getRedisTemplate() { return redisTemplate; } public void setRedisTemplate(RedisTemplate\n       \n         redisTemplate) { this.redisTemplate = redisTemplate; } @Override public void set(final K key, final V value, final long expiredTime) { BoundValueOperations\n        \n          valueOper = redisTemplate.boundValueOps(key); if (expiredTime valueOper = redisTemplate.boundValueOps(key); return valueOper.get(); } @Override public Object getHash(K key, String name){ Object res = redisTemplate.boundHashOps(key).get(name); return res; } @Override public void del(K key) { if (redisTemplate.hasKey(key)) { redisTemplate.delete(key); } } } \n        \n       \n      \n     \n    \n   <\/pre>\n<p>\u5b9e\u73b0\u7c7bRedisService\uff1a<\/p>\n<pre>\n@Service(\"redisService\") \npublic class RedisService extends AbstractRedisService\n   \n     { \n \n}\n   <\/pre>\n<p>\u5de5\u5177\u7c7bRedisTool\uff1a<\/p>\n<pre>\npublic class RedisTool { \n private static ApplicationContext factory;\n private static RedisService redisService;\n \n public static ApplicationContext getFactory(){\n  if (factory == null){\n   factory = new ClassPathXmlApplicationContext(\"classpath:redis.xml\");\n  }\n  return factory;\n } \n public static RedisService getRedisService(){\n  if (redisService == null){\n   redisService = (RedisService) getFactory().getBean(\"redisService\");\n  }  \n  return redisService;\n }\n\n}\n<\/pre>\n<p><span style=\"color: #ff0000\"><strong>4. \u67e5\u8be2\u529f\u80fd\u7684\u5b9e\u73b0<\/strong><\/span><\/p>\n<p>\u65b0\u5efa\u4e00\u4e2aAction\uff1aRClasQueryAction\uff0c\u8fd4\u56deRedis\u91cc\u9762\u6240\u6709\u7684\u8bfe\u7a0b\u6570\u636e\u3002<\/p>\n<pre>\n@SuppressWarnings(\"serial\")\npublic class RClasQueryAction extends ActionSupport {\n  RedisService rs = RedisTool.getRedisService();\n List\n   \n     claslist = new ArrayList\n    \n     (); Clas c; public String execute(){ if (rs != null){ System.out.println(\"RedisService : \" + rs); getAllClas(); } ServletActionContext.getRequest().setAttribute(\"claslist\", claslist); return SUCCESS; } private void getAllClas(){ claslist = new ArrayList\n     \n      (); int num = Integer.parseInt(rs.get(\"clas:count\")); for (int i=0; i\n      \n       <p>Struts\u7684\u8bbe\u7f6e\u548cjsp\u6587\u4ef6\u5c31\u4e0d\u8be6\u7ec6\u8bb2\u4e86\u3002<\/p> \n       <p><span style=\"color: #ff0000\"><strong>5. Redis\u6570\u636e\u5e93<\/strong><\/span><\/p> \n       <p>Redis\u6570\u636e\u5e93\u91cc\u9762\u7684\u5185\u5bb9\uff08\u4f7f\u7528\u7684\u662fRedis Desktop Manager\uff09\uff1a<\/p> \n       <p><img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167254475863b101f6985b6.png\" class=\"aligncenter\"><\/p> \n       <p>\u6700\u540e\u662f\u8fd0\u884c\u7ed3\u679c\uff1a<\/p> \n       <p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167254475863b101f6dcf55.png\" class=\"aligncenter\"><\/p> \n       <p>\u5f53\u7136\uff0c\u8fd9\u53ea\u662f\u5b9e\u73b0\u4e86\u4eceRedis\u67e5\u8be2\u6570\u636e\uff0c\u8fd8\u6ca1\u6709\u5b9e\u73b0\u5c06Redis\u4f5c\u4e3aMySQL\u7684\u7f13\u5b58\u3002<br><\/p> \n       <p><span style=\"color: #ff0000\"><strong>5. \u6dfb\u52a0\u529f\u80fd\u7684\u5b9e\u73b0<\/strong><\/span><\/p> \n       <p>\u65b0\u5efa\u4e00\u4e2aAction\uff1aRClasAction\uff0c\u5b9e\u73b0\u5411Redis\u6dfb\u52a0\u8bfe\u7a0b\u6570\u636e\uff0c\u5e76\u540c\u6b65\u5230MySQL\u3002<\/p> \n       <pre>\npackage com.school.action;\nimport java.util.ArrayList;\nimport java.util.HashMap;\nimport java.util.List;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport com.opensymphony.xwork2.ActionSupport;\nimport com.school.entity.Clas;\nimport com.school.redisclient.RedisService;\nimport com.school.redisclient.RedisTool;\nimport com.school.service.ClasService;\n@SuppressWarnings(\"serial\")\npublic class RClasAction extends ActionSupport { \n @Autowired\n private ClasService clasService; \n RedisService rs = RedisTool.getRedisService();\n List\n        \n          claslist = new ArrayList\n         \n          (); 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\n          \n            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); \/\/ \u589e\u52a0 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); \/\/ \u540c\u6b65\u5230MySQL clasService.saveClas(clas); } }\n          \n         \n        <\/pre>\n<p>clas:id\u662f\u4e00\u4e2aList\u7c7b\u578b\u7684Key-Value\uff0c\u8bb0\u5f55\u4e86\u6240\u6709\u7684\u8bfe\u7a0bID\uff0c\u53d6\u51fa\u6700\u540e\u4e00\u4e2aID\uff0c\u518d+1\uff0c\u4f5c\u4e3a\u589e\u52a0\u7684\u8bfe\u7a0b\u7684ID\uff0c\u540c\u65f6clas:count\u7684\u503c\u4e5f\u8981+1\u3002\u4f7f\u7528addHash()\u65b9\u6cd5\u5411Redis\u6dfb\u52a0\u4e86\u4e00\u4e2aHash\u7c7b\u578b\u7684Key-Value(\u4e5f\u5c31\u662f\u4e00\u95e8\u8bfe\u7a0b)\uff1a<\/p>\n<pre>\n  @SuppressWarnings({ \"unchecked\", \"rawtypes\" })\n  public synchronized void addHash(K key, HashMap map){\n   redisTemplate.opsForHash().putAll(key, map);\n  }<\/pre>\n<p>\u540c\u65f6\u5c06\u8be5\u95e8\u8bfe\u7a0b\u589e\u52a0\u5230MySQL\u3002<\/p>\n<p><span style=\"color: #ff0000\"><strong>6. \u5220\u9664\u529f\u80fd\u7684\u5b9e\u73b0<\/strong><\/span><\/p>\n<p>\u65b0\u5efa\u4e00\u4e2aAction\uff1aRClasDeleteAction\uff0c\u5b9e\u73b0\u5220\u9664Redis\u7684\u8bfe\u7a0b\u6570\u636e\uff0c\u5e76\u540c\u6b65\u5230MySQL\u3002<\/p>\n<pre>\npackage com.school.action;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport com.opensymphony.xwork2.ActionSupport;\nimport com.school.redisclient.RedisService;\nimport com.school.redisclient.RedisTool;\nimport com.school.service.ClasService;\n@SuppressWarnings(\"serial\")\npublic class RClasDeleteAction extends ActionSupport { \n @Autowired\n private ClasService clasService; \n RedisService rs = RedisTool.getRedisService() \n private int id;\n public int getId(){\n  return id;\n }\n public void setId(int id){\n  this.id=id;\n } \n public String execute(){ \n  deleteClas(id);\n  \/\/ \u540c\u6b65\u5230MySQL\n  clasService.deleteClas(id);\n  return SUCCESS;\n }\n private void deleteClas(int id){\n  \/\/ \u5220\u9664\n  rs.del(\"clas:\" + id);\n  \/\/ clas:count\n  int count = Integer.parseInt(rs.get(\"clas:count\"));\n  rs.set(\"clas:count\", String.valueOf(count-1), -1);\n  \/\/ clas:id\n  rs.delListItem(\"clas:id\", String.valueOf(id));\n }\n}<\/pre>\n<p>\u76f4\u63a5\u5220\u9664clas:id\uff0c\u518d\u5c06clas:count\u7684\u503c-1\uff0c\u8fd9\u4e24\u6b65\u6bd4\u8f83\u7b80\u5355\uff0c\u590d\u6742\u7684\u662f\u4ececlas:id\u4e2d\u5220\u9664\u8be5\u8bfe\u7a0b\u7684ID\uff0c\u4f7f\u7528\u4e86delListItem()\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a<\/p>\n<pre>\n  @Override\n  public synchronized void delListItem(K key, V value){\n   redisTemplate.opsForList().remove(key, 1, value);\n  }<\/pre>\n<p>redisTemplate.opsForList().remove()\u65b9\u6cd5\u7c7b\u4f3c\u4e8eLREM\u547d\u4ee4\u3002\u6700\u540e\u5728MySQL\u4e2d\u4e5f\u5220\u9664\u76f8\u540c\u7684\u8bfe\u7a0b\u3002<\/p>\n<p><span style=\"color: #ff0000\"><strong>7. \u4fee\u6539\u529f\u80fd\u7684\u5b9e\u73b0<\/strong><\/span><\/p>\n<p>\u65b0\u5efa\u4e00\u4e2aAction\uff1aRClasUpdateAction\uff0c\u5b9e\u73b0\u5220\u9664Redis\u7684\u8bfe\u7a0b\u6570\u636e\uff0c\u5e76\u540c\u6b65\u5230MySQL\u3002<\/p>\n<pre>\npackage com.school.action;\n\nimport java.util.HashMap;\n\nimport org.springframework.beans.factory.annotation.Autowired;\n\nimport com.opensymphony.xwork2.ActionSupport;\nimport com.school.entity.Clas;\nimport com.school.redisclient.RedisService;\nimport com.school.redisclient.RedisTool;\nimport com.school.service.ClasService;\n\n@SuppressWarnings(\"serial\")\npublic class RClasUpdateAction extends ActionSupport{ \n @Autowired\n private ClasService clasService; \n RedisService rs = RedisTool.getRedisService(); \n private Clas clas;\n public Clas getClas() {\n  return clas;\n }\n public void setClas(Clas clas) {\n  this.clas = clas;\n }\n  @SuppressWarnings({ \"unchecked\", \"rawtypes\" })\n public String execute(){\n  HashMap hashmap = new HashMap();\n  hashmap.put(\"ID\", String.valueOf(clas.getId()));\n  hashmap.put(\"NAME\", clas.getName());\n  hashmap.put(\"COMMENT\", clas.getComment());\n  rs.putHash(\"clas:\" + clas.getId(), hashmap);\n  \/\/ \u540c\u6b65\u5230MySQL\n  clasService.updateClas(clas);\n  return SUCCESS;\n }\n\n}\n<\/pre>\n<p>\u4f7f\u7528\u4e86putHash()\u65b9\u6cd5\u6765\u66f4\u65b0\uff1a<\/p>\n<pre>\n  @SuppressWarnings({ \"rawtypes\", \"unchecked\" })\n  @Override\n  public synchronized void putHash(K key, HashMap map){\n   redisTemplate.boundHashOps(key).putAll(map);\n  }<\/pre>\n<p>&nbsp;\u540c\u65f6\u5728MySQL\u505a\u76f8\u540c\u7684\u66f4\u65b0\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8be6\u89e3SSH\u6846\u67b6\u548cRedis\u7684\u6574\u5408&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201266","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201266"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201266\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}