Redis缓存Mysql模拟用户登录Java实现实例

这段时间在研究Redis,作为缓存界的新宠,现在使用它的公司越来越多。本文使用的是最新稳定版Redis3.0.实现的具体逻辑是:

    1. 用户登录首先判断是否在redis缓存中,如果在redis缓存中,直接登录成功;

    2. 若用户未在redis缓存,则访问Mysql,判断用户是否存在,如果不存在,则提示用户注册;如果存在,则登录成功;

    3. 在mysql存在并登录成功的同时,将改条数据用Redis Hash类型进行缓存,并设置过期时间为30分钟;

    4. 设置redis最大内存,若超出内存范围,则使用FIFO方式进行清除。

本文实现方式为最简单的模拟方式,有的代码有进一步封装得必要。

一、首先创建两个类,一个类连接Mysql,一个类连接Redis,并复写相关方法:

 
  1. public class Mysql {

  2. public Connection conn;

  3. {

  4. try {

  5. Class.forName("com.mysql.jdbc.Driver");

  6. conn=DriverManager.getConnection("jdbc:mysql://localhost/spring","root","root");

  7. } catch (ClassNotFoundException e) {

  8. e.printStackTrace();

  9. } catch (SQLException e) {

  10. e.printStackTrace();

  11. }

  12. }

  13.  
  14. }

 
  1. public class Redis extends Jedis {

  2.  
  3. public Jedis redis;

  4. {

  5. redis = new Jedis("192.168.217.128", 6379);

  6. redis.auth("root");

  7. }

  8.  
  9. // public static void main(String[] args) {

  10. // System.out.println(redis.get("name"));

  11. // System.out.println(redis.keys("*"));

  12. // // redis.sinter(keys);

  13. // }

  14.  
  15. public String get(String key) {

  16. return redis.get("name");

  17. }

  18.  
  19. public String set(String key, String value) {

  20. return redis.set(key, value);

  21. }

  22.  
  23. public Long del(String... keys) {

  24. return redis.del(keys);

  25. }

  26.  
  27. // 键值增加字符

  28. public Long append(String key, String str) {

  29. return redis.append(key, str);

  30. }

  31.  
  32. public Boolean exists(String key) {

  33. return redis.exists(key);

  34. }

  35.  
  36. // Need research

  37. public Long setnx(String key, String value) {

  38. return redis.setnx(key, value);

  39. }

  40.  
  41. public String setex(String key, String value, int seconds) {

  42. return redis.setex(key, seconds, value);

  43. }

  44.  
  45. public Long setrange(String key, String str, int offset) {

  46. return redis.setrange(key, offset, str);

  47. }

  48.  
  49. public List<String> mget(String... keys) {

  50. return redis.mget(keys);

  51. }

  52.  
  53. public String mset(String... keys) {

  54. return redis.mset(keys);

  55. }

  56.  
  57. public Long msetnx(String... keysvalues) {

  58. return redis.msetnx(keysvalues);

  59. }

  60.  
  61. public String getset(String key, String value) {

  62. return redis.getSet(key, value);

  63. }

  64.  
  65. public String hmset(String key, Map<String, String> hash) {

  66. return redis.hmset(key, hash);

  67. }

  68.  
  69. public Map<String, String> hgetall(String key) {

  70. return redis.hgetAll(key);

  71. }

  72.  
  73. public String hget(final String key, final String field) {

  74. return redis.hget(key, field);

  75. }

  76.  
  77. public Long hset(final String key, final String field, final String value) {

  78. return redis.hset(key, field, value);

  79. }

  80.  
  81. public Long expire(final String key, final int seconds) {

  82. return redis.expire(key, seconds);

  83. }

  84.  
  85. public Boolean hexists(final String key, final String field) {

  86. return redis.hexists(key, field);

  87. }

  88.  
  89. }



二、逻辑具体实现:

 
  1. public class Main {

  2. Mysql mysql=new Mysql();

  3. Redis redis=new Redis();

  4. ResultSet rs=null;

  5.  
  6. //模拟登陆缓存

  7. @Test

  8. public void redisLogin() throws SQLException{

  9. //正常业务的ID是通过UI的request.getParamenter()获取

  10. String id="9028935b527d22cc01527d235aea0142";

  11. String sql="select * from user where id_='"+id+"'";

  12. String username;

  13. if(redis.hexists("user_"+id, "username_")){

  14. username=redis.hget("user_"+id, "username_");

  15. System.out.println("Welcome Redis! User "+username+" login success");

  16. }else{

  17. rs=mysql.conn.createStatement().executeQuery(sql);

  18. if(rs.next()==false){

  19. System.out.println("Mysql no register, Please register first");

  20. }else{

  21. username=rs.getString("username_");

  22. System.out.println("Welcome Mysql ! User "+username+" login success");

  23. redis.hset("user_"+id, "username_", username);

  24. //30分钟未操作就过期

  25. redis.expire("user_"+id, 1800);

  26. }

  27. }

  28.  
  29. }

  • }

猜你喜欢

转载自blog.csdn.net/chengxuyuan_110/article/details/81126512