• Redis和Ehcache的本质区别


    转https://blog.csdn.net/shenbushen/article/details/52140078

    第一:两者之间的介绍

    Redis:属于独立的运行程序,需要单独安装后,使用JAVA中的Jedis来操纵。因为它是独立,所以如果你写个单元测试程序,放一些数据在Redis中,然后又写一个程序去拿数据,那么是可以拿到这个数据的。,

    ehcache:与Redis明显不同,它与java程序是绑在一起的,java程序活着,它就活着。譬如,写一个独立程序放数据,再写一个独立程序拿数据,那么是拿不到数据的。只能在独立程序中才能拿到数据。

    第二:使用及各种配置:

    两者都可以集群:

    1.Redis可以做主从来集群,例如,在A电脑上装个Redis,作为主库;在其他电脑上装Redis,作为从库;这样主库拥有读和写的功能,而从库只拥有读的功能。每次主库的数据都会同步到从库中。

    1.默认方式启动

    Linux下使用Redis  
    安装:从官网上下载tar.gz格式的包,然后使用tar zxvf redis-2.8.24.tar.gz命令解压,然后进入Redis文件夹目录下的src目录,使用make编译一下  
    1.开启:进入/usr/local/redis-3.2.1/src  
    然后./redis-server
    

    2.如果我们想修改端口,设置密码:那么得修改配置文件的redis.conf

    port 6379                                         //端口修改
    requirepass redis123                  //设置密码  redis123为密码
    配置主从:主库的配置文件不用修改,从库的配置文件需要修改,因为从库需要绑定主库,以便可以获取主库的数据
    slaveof 192.168.1.100 6379                              //主库的IP地址和端口号
    masterauth redis123                                      //主库设定的密码
    

    3.要让配置文件的属性生效,那么启动的redis的时候,要将配置文件加上去

    进入/usr/local/redis-3.2.1/src
    然后 ./redis-server  redis.conf
    那么将成功的启动redis,如果没有加入配置的话,按照普通方式启动的话,端口仍然还是6379
    

    4.客户端连接远程的Redis

    第一步:在远程端处设置密码:config set requirepass 123       //123为密码
    第二步:可以在客户端登录  redis-cli.exe -h 114.215.125.42 -p 6379 
    第三步:认证:auth 123                                       //123为密码
    本地端设置密码后,要使用密码登录;如果Redis重启的话,密码需要重新设置
    

    5.主从配置后,为保证主库写的能力,一般不在主库做持久化,而是在从库做持久化:

    主库配置:
    将save注释,不使用rdb
    # save 900 1
    # save 300 10
    # save 60 10000
    appendonly no        不使用aof
    
    从库配置:
    save 900 1
    save 300 10
    save 60 10000
    appendonly yes
    
    这样做的优缺点:
    优点:保证了主库写的能力。
    缺点:主库挂掉后,重启主库,然后进行第一次写的动作后,主库会先生成rdb文件,然后传输给从库,从而覆盖掉从库原先的rdb文件,造成数据丢失。但是第二次写的时候,主库会以快照方式直接传数据给从库,不会重新生成rdb文件。
    
    解决方案:先复制从库中的数据到主库后,再启动主库。

    使用:引入jedis包

    <dependency>  
        <groupId>redis.clients</groupId>  
        <artifactId>jedis</artifactId>  
        <version>2.7.3</version>  
    </dependency>  

    简单的写个类玩玩吧

    public class RedisMain {  
                
        public static void main(String [] str)  
        {  
              
             Jedis jedis = new Jedis("114.215.125.42",6379);  
             jedis.auth("123");     //密码认证  
              System.out.println("Connection to server sucessfully");  
              //查看服务是否运行  
              jedis.set("user","namess");  
             // System.out.println("Server is running: "+jedis.ping());  
              System.out.println(jedis.get("user").toString());  
              jedis.set("user","name");  
              System.out.println(jedis.get("user"));              
      
        }  
      
    }  

    下面说说Ehcache:

    Ehcache的使用:

    1.首先引入包

           <dependency>  
               <groupId>net.sf.ehcache</groupId>  
               <artifactId>ehcache-core</artifactId>  
               <version>2.6.6</version>  
           </dependency>  
       
           <dependency>  
               <groupId>org.slf4j</groupId>  
               <artifactId>slf4j-log4j12</artifactId>  
               <version>1.6.6</version>  
           </dependency>     

    2.创建一个ehcache.xml文件,里面配置cache的信息,这个配置是包含了集群的配置:与192.168.93.129:40001的机器集群了:Ip为192.168.93.129机子的配置要将rmiUrls对应的数据改为这个配置文件的机子的IP地址,和对应的缓存名字

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:noNamespaceSchemaLocation="ehcache.xsd">  
        <cacheManagerPeerProviderFactory   
       class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"  
    properties="peerDiscovery=manual,rmiUrls=//192.168.93.129:40001/demoCache"/>           <!--另一台机子的ip缓存信息-->  
    <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"  
    properties="hostName=localhost,port=40001,socketTimeoutMillis=2000" />                  <!--hostName代表本机子的ip-->     
      <diskStore path="java.io.tmpdir"/>  
      <defaultCache  
        maxElementsInMemory="10000"  
        maxElementsOnDisk="0"  
        eternal="true"  
        overflowToDisk="true"  
        diskPersistent="false"  
        timeToIdleSeconds="0"  
        timeToLiveSeconds="0"  
        diskSpoolBufferSizeMB="50"  
        diskExpiryThreadIntervalSeconds="120"  
        memoryStoreEvictionPolicy="LFU" >  
         <cacheEventListenerFactory    
                    class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>                
        </defaultCache>  
      <cache name="demoCache"  
        maxElementsInMemory="100"  
        maxElementsOnDisk="0"  
        eternal="false"  
        overflowToDisk="false"  
        diskPersistent="false"  
        timeToIdleSeconds="119"  
        timeToLiveSeconds="119"  
        diskSpoolBufferSizeMB="50"  
        diskExpiryThreadIntervalSeconds="120"  
        memoryStoreEvictionPolicy="FIFO"  >  
        <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>          
    <!--监听这个cache-->  
        </cache>  
    </ehcache>  

    配置完后写代码:

    放数据:

    @RequestMapping("/testehcache.do")  
        public void testehcache(HttpServletResponse response) throws IOException  
        {  
            URL url = getClass().getResource("ehcache.xml");   
             CacheManager singletonmanager = CacheManager.create(url);  
            Cache cache = singletonmanager.getCache("demoCache");  
            //使用缓存  
            Element element = new Element("key1", "value1");  
            cache.put(element);  
            cache.put(new Element("key2", "value2"));  
             
            response.getWriter().println("我存放了数据");  
        } 

    拿数据:

    @RequestMapping("/getcache.do")  
        public void getcache(HttpServletResponse response) throws IOException  
        {  
            CacheManager singletonmanager = CacheManager.create();   
            Cache cache = singletonmanager.getCache("demoCache");  
            String one=cache.get("key1").getObjectValue().toString();  
            String two=cache.get("key2").getObjectValue().toString();  
            response.getWriter().println(one+two);  
        } 

    配置集群后,A机器放数据,在B机器上能拿到数据,B机器放数据,A机器也可以拿到数据

    结束。

     
  • 相关阅读:
    Kill Processes in Linux
    How to Setup Chroot SFTP in Linux (Allow Only SFTP, not SSH)
    156 Useful Run Commands
    6
    pandas groupby合并列字符串
    一个ROS配置的脚本
    Mybatis 学习记录
    Android搭建code server
    CF 1616D. Keep the Average High
    第七章:(1)Redis 的发布订阅
  • 原文地址:https://www.cnblogs.com/fr13nds/p/11090025.html
Copyright © 2020-2023  润新知