Comparison and use of Redis and Ehcache in JAVA __java

Source: Internet
Author: User
Tags auth

First: The introduction between the two

Redis: A standalone running program that needs to be installed separately and manipulated using Jedis in Java. Because it is independent, so if you write a unit test program, put some data in the Redis, and then write a program to get the data, then you can get the data. ,

Ehcache: Unlike Redis, which is tied to the Java program, the Java program is alive, and it's alive. For example, write a separate program to put data, and then write a stand-alone program to get the data, then can not get the data. Data can only be obtained in stand-alone programs.


Second: Use and various configurations:

Both can be clustered:

1.Redis can make the decision to never cluster, for example, on a computer with a redis, as the main library, on other computers on the Redis, as from the library, so the main library has read and write functions, and the library has only read functionality. Data from the main library is synchronized to the library every time.

1. The default way to start

Linux using Redis
installation: Download the tar.gz format package from the official web, then unzip it using the tar zxvf redis-2.8.24.tar.gz command, then go to the SRC directory in the Redis folder directory and compile

with make 1. Open: Enter/usr/local/redis-3.2.1/src
and then./redis-server


2. If we want to modify the port, set the password: then have to modify the configuration file redis.conf

Port 6379//Ports modification

Requirepass redis123//Set password redis123 to password


Configuration files from the main library need not be modified, the configuration file from the library needs to be modified because the main library needs to be bound from the library so that the main library's data can be obtained

Slaveof 192.168.1.100 6379//main library IP address and port number
Masterauth redis123//Main Library set password

3. To allow the properties of the profile to take effect, when the Redis is started, add the configuration file to the

Enter/USR/LOCAL/REDIS-3.2.1/SRC

and then./redis-server redis.conf

then the successful boot Redis, if not joined the configuration, the normal way to start, the port is still 6379.


4. Client Connection remote Redis
Step one: Set the password at the remote end: Config set requirepass 123//123 for password
Step two: You can log on to the client redis-cli.exe-h 114.215.125.42-p 6379
Step three: Authentication: Auth 123//123 for password
After setting the password on the local side, log in using the password, and if the Redis reboot, the password needs to be
reset.

5. Master-slave configuration, in order to ensure the ability to write the main library, generally not in the main library to do the persistence, but in the library to do the persistence:

Main Library configuration:

will save comment, do not use RDB

# Save 900 1
# Save 300 10
# Save 60 10000


AppendOnly no use aof


To configure from a library:

Save 900 1
Save 300 10
Save 60 10000

AppendOnly Yes


the pros and cons of doing this:

advantages: To ensure the ability to write the main library.

disadvantage: After the main library is hung up, restart the main library, and then make the first write of the action after the master Couhis into the Rdb file, and then transmit it from the library, thus overwriting the original Rdb file from the library, resulting in data loss. But for the second time, the main library will send data directly from the library as a snapshot, without rebuilding the Rdb file.

Solution: First copy the data from the library to the main library, and then start the main library.

Use:

Introduction of Jedis Package

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

Simply write a class and play it.

public class Redismain {public
	
	
	static void Main (String [] str)
	{
		
		 Jedis Jedis = new Jedis ("114.215.125.42", 637 9);
		 Jedis.auth ("123");     Password Authentication
	      System.out.println ("Connection to Server sucessfully");
	      Check to see if the service is running
	      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"));
	      

	}

Redis complete.


Let's say Ehcache:

Use of Ehcache:

1. First introduce the package

<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. Create a Ehcache.xml file, which configures cache information, this configuration contains the configuration of the cluster: With the 192.168.93.129:40001 of the machine cluster: IP for the 192.168.93.129 configuration to the rmiurls of the corresponding data to the configuration file IP address, and the corresponding cache name

<ehcache xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation= "ehcache.xsd" > <cachemanagerpeerproviderfactory class= "Net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" p roperties= "Peerdiscovery=manual,rmiurls=//192.168.93.129:40001/democache"/> <!--IP caching information for another machine--> &LT;CAC Hemanagerpeerlistenerfactory class= "Net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" properties= " hostname=localhost,port=40001,sockettimeoutmillis=2000 "/> <!--HostName represents the ip--> of this machine <disk Store path= "Java.io.tmpdir"/> <defaultcache maxelementsinmemory= "10000" maxelementsondisk= "0" eternal= "True" overflowtodisk= "true" diskpersistent= "false" timetoidleseconds= "0" timetoliveseconds= "0" Disksp oolbuffersizemb= "diskexpirythreadintervalseconds=" memorystoreevictionpolicy= "LFU" > <cache Eventlistenerfactory class="Net.sf.ehcache.distribution.RMICacheReplicatorFactory"/> </defaultCache> <cache name= "dem Ocache "maxelementsinmemory=" maxelementsondisk= "0" eternal= "false" overflowtodisk= "false" Diskper Sistent= "false" timetoidleseconds= "119" timetoliveseconds= "119" diskspoolbuffersizemb= "a" Diskexpirythrea dintervalseconds= "memorystoreevictionpolicy=" "FIFO" > <cacheeventlistenerfactory class= "NET.SF.EHCAC He.distribution.RMICacheReplicatorFactory "/> <!--monitor this cache--> </cache> </ehcache>

Write code after configuration:

Put data:

@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");
        Use cache
        Element element = new Element ("Key1", "value1");
        Cache.put (element);
        Cache.put (New Element ("Key2", "value2"));
       
        Response.getwriter (). println ("I have data stored");
	


Get the data:

@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);
	

After configuring the cluster, a machine puts data, can get data on B machine, B machine puts data, a machine can get data



This article ends .....




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.