zoukankan      html  css  js  c++  java
  • Redis入门学习笔记一

    Redis 简要描述:

      1.  Redis 是啥 ?

           Redis 英文名称全称为: Remote Dictionary Server ,中译为远程字典服务器。 是一款区分于磁盘数据库如(Mysql)的采用Key-Value键值对的字典结构的缓存数据库。

      2. Redis有什么作用?

           Redis作为一款内存数据库,其最大的有点就是高速,对于那些高频访问的数据,进行加缓存。Redis加载缓存的时候使用的LRU机制,对于热点数据将会持续保留,其他的将会被淘汰。

        

    Redis涉及到的LRU简要源码解析算法实现:

     1 package com.mysql.jdbc.util;
     2 
     3 import java.util.LinkedHashMap;
     4 import java.util.Map.Entry;
     5 
     6 public class LRUCache extends LinkedHashMap {
     7     private static final long serialVersionUID = 1L;
     8     protected int maxElements;
     9 
    10     public LRUCache(int maxSize) {
    11         super(maxSize, 0.75F, true);
    12         this.maxElements = maxSize;
    13     }
    14 
    15     protected boolean removeEldestEntry(Entry eldest) {
    16         return this.size() > this.maxElements;
    17     }
    18 }

         注解:  Lru算法使用Java jdk提供的LinkedHashMap实现 

     1 package com.hbut.util;
     2 
     3 import java.util.LinkedHashMap;
     4 import java.util.Map;
     5 
     6 /**
     7  * @Author XiJun.Gong
     8  * @DATE 2016/6/11.
     9  * aim:   com.hbut.util
    10  */
    11 public class Lru extends LinkedHashMap {
    12     private static final long serialVersionUID = 1L;
    13     private int maxElements = 5; //default size is 5
    14 
    15     public int getMaxElements() {
    16         return maxElements;
    17     }
    18 
    19     public void setMaxElements(int maxElements) {
    20         this.maxElements = maxElements;
    21     }
    22 
    23     /**
    24      *  if size > maxElements remove this old entry and
    25      *  add new entry
    26      * @param eldest
    27      * @return true or false
    28      */
    29     protected boolean removeEldestEntry(Map.Entry eldest) {
    30 
    31         return size() > this.maxElements;
    32     }
    33 }
    View Code

     Redis如何使用java是测试用例:

    使用Maven配置pom.xml

    1    <!--Redis cache -->
    2             <dependency>
    3                 <groupId>redis.clients</groupId>
    4                 <artifactId>jedis</artifactId>
    5                 <version>${redis.clients.version}</version>
    6                 <type>jar</type>
    7                 <scope>compile</scope>
    8             </dependency>

    window环境下测试流程:

       window Redis版下载环境:   https://github.com/MSOpenTech/redis

        Redis 有五个执行程序:

           redis-server server服务器,需要启动它
           redis-client redis命令行客户端
           redis-benchmark 性能测试工具
          redis-check-aof/rdb rdb/aof修复工具,aof ->AppendOnly File

       启动 redis-server服务器,出现如下界面

     

      java 代码测试

            

     1 package com.hbut.util;
     2 
     3 import com.google.common.collect.Maps;
     4 import org.junit.Before;
     5 import org.junit.Test;
     6 import redis.clients.jedis.Jedis;
     7 import redis.clients.jedis.JedisPool;
     8 import redis.clients.jedis.JedisPoolConfig;
     9 
    10 import java.io.*;
    11 import java.util.Iterator;
    12 import java.util.List;
    13 import java.util.Map;
    14 
    15 /**
    16  * Created by XiJun.Gong on 14-2-28.
    17  */
    18 public class TestRedis {
    19 
    20 
    21     JedisPool pool;
    22     Jedis jedis;
    23 
    24     /**
    25      * connection
    26      */
    27     @Before
    28     public void init() {
    29         pool = new JedisPool(new JedisPoolConfig(), "localhost");
    30         jedis = pool.getResource();
    31         //jedis.auth("*******"); //密码验证
    32     }
    33 
    34 
    35     /**
    36      * 存储字符串,设置失效时间
    37      */
    38     @Test
    39     public void RedisTest() throws InterruptedException {
    40         jedis.set("google", "entry1");
    41         System.out.println(jedis.get("google"));
    42         jedis.expire("google", 3);  //设置过期时间
    43         Thread.sleep(5000);
    44         System.out.println(jedis.get("google"));
    45     }
    46 
    47 
    48     /**
    49      * CRUD for String
    50      */
    51     @Test
    52     public void redisCRUD() {
    53         /**add**/
    54         jedis.set("key", "google");
    55         System.out.println(jedis.get("key"));
    56         /**delete**/
    57         jedis.del("key");
    58         System.out.println(jedis.get("key"));
    59         /*modify*/
    60         jedis.append("key", "Qunar.com");
    61         System.out.println(jedis.get("key"));
    62         /**another method**/
    63         jedis.set("key", "Tencent");
    64         System.out.println(jedis.get("key"));
    65 
    66         /**Multi value  mapping key**/
    67         jedis.mset("key", "google", "tencent", "Qunar");
    68         System.out.println(jedis.mget("key", "google"));
    69 
    70         /*for map*/
    71         Map<String, String> user = Maps.newHashMap();
    72         user.put("huifeidmeng", "Qunar");
    73         jedis.hmset("user", user);
    74         List<String> rsmap = jedis.hmget("user", "key");
    75         System.out.println(rsmap);
    76 
    77     }
    78 
    79 }
  • 相关阅读:
    jmeter bean shell断言加密的响应信息(加密接口测试二)
    java ID3算法
    MPI常用函数
    数据结构——单链表
    RBM代码注释c++
    MPI_一个简单的消息传递
    电路测试
    java KNN算法
    [转]矩阵分解在推荐系统中的应用
    java EM算法
  • 原文地址:https://www.cnblogs.com/gongxijun/p/5575372.html
Copyright © 2011-2022 走看看