Redis installation process and the use of the SSM

Source: Internet
Author: User
Tags redis serialization redis server
Redis Installation Process

Download installation package, unzip, Mac system put folder under/usr/local folder

Open the terminal and install server-side servers first
Terminal $ cd/usr/local/redis/

Go to the file and enter the Redis.config file

Find Requirepass Delete Front #
and modify the password behind the stitching
Save into terminal
Enter command
Terminal $ CD src/
Terminal $./redis-server. /redis.conf
Access to Redis server side
Ctrl + C exit

(If you are prompted without permission in the procedure, add sudo before the command) but in the course of use do not quit and then open a terminal, install client trial
Command
Terminal $ cd/usr/local/redis/src/
Terminal $./redis-cli-a Password
To the Redis client.

Commands for manipulating Redis clients
*
Front is the IP address: port number
Keys * Query for all key
*
Set key and value
* Get Name
Get value from key
* FLUSHDB
Clears data in Redis memory, but does not recommend frequent use, affecting server performance
* del name
You can also delete data by key
* Exit
Exit Redis and SSM in conjunction with

1. Build a SSM project and connect to the database
Below I mainly introduce and Redis related configuration
2. Create Rediscache class, this class is the core of Redis

Package Com.dhl.redis; Redis Cache//Jedis Redis for Java/** * Serialization is the data * will not serialize the class structure * but will serialize the relationship (data corresponding to the class structure)/public class Rediscache implement S cache {///Redis MyBatis Level Two cache implementation//Need intermediate class to static note private static Jedisconnectionfactory jedisconnectionfact

    Ory;

    Private final String ID;


    Private final Readwritelock Readwritelock = new Reentrantreadwritelock (); public static void Setjedisconnectionfactory (Jedisconnectionfactory jedisconnectionfactory) {Rediscache.jedisconn
    Ectionfactory = jedisconnectionfactory;  The Cache object Public Rediscache (String ID) {if (id = = NULL) {//is thrown] {////////throws illegal parameter exceptions by identity construction throw
        New IllegalArgumentException ("Cache ID cannot be empty");
    } this.id = ID;
    /** * Get Cache object's unique standard * * @return/public String getId () {return this.id;  /** * Saves the key/value to the cached object/public void Putobject (object key, Object value) {Jedisconnection JedisconNection = null;


            try {//Operation Readis to save jedisconnection = (jedisconnection) jedisconnectionfactory.getconnection ();

            Create a serialization tool redisserializer<object> serializer = new Jdkserializationredisserializer ();
        Create the connection, save the key's value Jedisconnection.set (Serializer.serialize (key), serializer.serialize (value));
        }catch (jedisconnectionexception e) {e.printstacktrace ();
            }finally {if (jedisconnection!= null) {jedisconnection.close (); 
        The public object GetObject (object key) {//Gets value object result = null from the cached object via key;
        Jedisconnection jedisconnection = null;

            try {//Establish connection Jedisconnection = (jedisconnection) jedisconnectionfactory.getconnection ();

            redisserializer<object> serializer= new Jdkserializationredisserializer (); byte[] bytes = JediscoNnection.get (Serializer.serialize (key)); 1. Serializes key to//2. Gets the serialized value//3 via connection.


            Deserializes the serialized value to result = Serializer.deserialize (bytes); /** * Serialization is the data * will not serialize the class structure * but will serialize the relationship (data corresponding to the class structure)/}catch (Jedi
        Sconnectionexception e) {e.printstacktrace ();
            }finally {if (jedisconnection!= null) {jedisconnection.close ();
    } return result;

        /** * is not a method that needs to be implemented and is not called by the core framework. * Method Action: According to key remove corresponding Value/public object Removeobject (object key) {
        Object result = null;
        Jedisconnection jedisconnection = null;

            try {//Establish connection Jedisconnection = (jedisconnection) jedisconnectionfactory.getconnection ();

            redisserializer<object> serializer= new Jdkserializationredisserializer (); Remove saved to value result = Jedisconnection.expire (Serializer.serialize (key), 0);
        }catch (jedisconnectionexception e) {e.printstacktrace ();
            }finally {if (jedisconnection!= null) {jedisconnection.close ();
    } return result;
        //Clear cache (Operations affect performance, inefficiencies, do not recommend frequent cleanup) public void Clear () {jedisconnection jedisconnection = null;
            try {jedisconnection = (jedisconnection) jedisconnectionfactory.getconnection ();
            Clear Cache Jedisconnection.flushdb ();

        Clear Settings Jedisconnection.flushall (); }catch (jedisconnectionexception e) {}finally {if (jedisconnection!= null) {Jedisco
            Nnection.close ();  /** * Gets the number of key/value accessed in the cached object * @return/public int getsize () {Integer
        Count = 0;
        Jedisconnection jedisconnection = null; try {jedisconnectiOn = (jedisconnection) jedisconnectionfactory.getconnection ();

        Count = integer.valueof (Jedisconnection.dbsize (). toString ()); }catch (jedisconnectionexception e) {}finally {if (jedisconnection!= null) {Jedisco
            Nnection.close ();
    } return count; //Get read/write lock/from MyBatis version 3.2.6, this method is no longer called by the frame core/all required locks, it must be provided by the caching provider to the public Readwritelock Getreadwritelock
    () {return this.readwritelock;
 }
}

3. Create Intermediate Class

Package Com.lanou.redis;

public class Rediscachetransfer {

    @Autowired public
    void Setjedisconnectionfactory (jedisconnectionfactory Jedisconnectionfactory) {
        rediscache.setjedisconnectionfactory (jedisconnectionfactory);
    }
}

4.redis configuration File--(1) redis.properties

# Redis Configuration
redis.host=127.0.0.1
redis.port=6379
redis.pass=111111

redis.maxidl=300
redis.maxactive=600
redis.maxwait=1000

5.redis configuration File--(2) Ssm-redis.xml
This profile can also be written with the Ssm-mybatis.xml configuration file

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd "> <bean id=" propertyconfigurer "class="
            Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> <property name=" Locations "> <list> <value>classpath:redis.properties</value> &LT;VALUE&GT;CL asspath:db.properties</value> </list> </property> </bean> <!--building RE Data sources of Dis--> <bean id= "Poolconfig" class= "Redis.clients.jedis.JedisPoolConfig" > <property name= "MaxI" Dle "value=" ${redis.maxidl} "/> <property name=" maxtotal "value=" ${redis.maxactive} "/> &Lt;property name= "Maxwaitmillis" value= "${redis.maxwait}"/> </bean> <bean id= "Jedisconnectionfactory" class= "Org.springframework.data.redis.connection.jedis.JedisConnectionFactory" P:hostname= "${redis.ho ST} "p:port=" ${redis.port} "p:password=" ${redis.pass} "p:poolconfig-ref=" Poolconfig "/> <bean id=" RedisCach Etranfer "class=" Com.lanou.redis.RedisCacheTransfer "> <property name=" jedisconnectionfactory "ref=" Jedisconn Ectionfactory "/> </bean> </beans>

6.mybatis configuration file-–mybatis-config.xml

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config . DTD "> <configuration> <settings> <!--globally enables or disables any caches configured I N any mapper under this configuration--> <setting name= "cacheenabled" value= "true"/> <!--Set S number of seconds the driver would wait for a response from the database--> <setting name= "Defaultstat Ementtimeout "value=" 3000 "/> <!--enables automatic mapping from classic database column names a_column to C 
        Amel Case Classic Java property names Acolumn--> <setting name= "Mapunderscoretocamelcase" value= "true"/> <!--allows JDBC support for generated keys.
        A compatible driver is required. This setting forces generated keys to is used if set to true, as some drivers deny compatibility but still work --> <setting name= "Usegeneratedkeys" value= "true"/> <!--query, close the associated object even when loading, in order to improve performance--> <setting name= "lazyloadingenabled" value= "false"/> <!--allows multiple result sets, the default is true--> <setting name = "Multipleresultsetsenabled" value= "true"/> <!--sets the shape of the associated object loading, where the fields are loaded on demand. Instead of loading all the fields, you have the SQL to determine the--> &L

T;setting name= "aggressivelazyloading" value= "true"/> </settings> <!--Continue going here--> </configuration>

7. Finally, do not forget to configure Ssm-redis.xml in the Web.xml configuration file

8. Can write query statements to test, run successfully after we can go to the terminal, using Redis client, through key * can be found in the memory of the data Redis installation process

Download installation package, unzip, Mac system put folder under/usr/local folder

Open the terminal and install server-side servers first
Terminal $ cd/usr/local/redis/

Go to the file and enter the Redis.config file

Find Requirepass Delete Front #
and modify the password behind the stitching
Save into terminal
Enter command
Terminal $ CD src/
Terminal $./redis-server. /redis.conf
Access to Redis server side
Ctrl + C exit

(If you are prompted without permission in the procedure, add sudo before the command) but in the course of use do not quit and then open a terminal, install client trial
Command
Terminal cd/usr/local/redis/src/Terminal cd/usr/local/redis/src/Terminal./redis-cli-a Password
To the Redis client.

Commands for manipulating Redis clients
*
Front is the IP address: port number
Keys * Query for all key
*
Set key and value
*
Get value from key
*
Clears data in Redis memory, but does not recommend frequent use, affecting server performance
*
You can also delete data by key
*
Exit Redis and SSM in conjunction with

1. Build a SSM project and connect to the database
below I mainly introduce and redis related to the configuration
2. Create Rediscache class, which is the core of Redis

Package Com.dhl.redis; Redis Cache//Jedis Redis for Java/** * Serialization is the data * will not serialize the class structure * but will serialize the relationship (data corresponding to the class structure)/public class Rediscache implement S cache {///Redis MyBatis Level Two cache implementation//Need intermediate class to static note private static Jedisconnectionfactory jedisconnectionfact

    Ory;

    Private final String ID;


    Private final Readwritelock Readwritelock = new Reentrantreadwritelock (); public static void Setjedisconnectionfactory (Jedisconnectionfactory jedisconnectionfactory) {Rediscache.jedisconn
    Ectionfactory = jedisconnectionfactory;  The Cache object Public Rediscache (String ID) {if (id = = NULL) {//is thrown] {////////throws illegal parameter exceptions by identity construction throw
        New IllegalArgumentException ("Cache ID cannot be empty");
    } this.id = ID;
    /** * Get Cache object's unique standard * * @return/public String getId () {return this.id;  /** * Saves the key/value to the cached object/public void Putobject (object key, Object value) {Jedisconnection JedisconNection = null;


            try {//Operation Readis to save jedisconnection = (jedisconnection) jedisconnectionfactory.getconnection ();

            Create a serialization tool redisserializer<object> serializer = new Jdkserializationredisserializer ();
        Create the connection, save the key's value Jedisconnection.set (Serializer.serialize (key), serializer.serialize (value));
        }catch (jedisconnectionexception e) {e.printstacktrace ();
            }finally {if (jedisconnection!= null) {jedisconnection.close (); 
        The public object GetObject (object key) {//Gets value object result = null from the cached object via key;
        Jedisconnection jedisconnection = null;

            try {//Establish connection Jedisconnection = (jedisconnection) jedisconnectionfactory.getconnection ();

            redisserializer<object> serializer= new Jdkserializationredisserializer (); byte[] bytes = JediscoNnection.get (Serializer.serialize (key)); 1. Serializes key to//2. Gets the serialized value//3 via connection.


            Deserializes the serialized value to result = Serializer.deserialize (bytes); /** * Serialization is the data * will not serialize the class structure * but will serialize the relationship (data corresponding to the class structure)/}catch (Jedi
        Sconnectionexception e) {e.printstacktrace ();
            }finally {if (jedisconnection!= null) {jedisconnection.close ();
    } return result;

        /** * is not a method that needs to be implemented and is not called by the core framework. * Method Action: According to key remove corresponding Value/public object Removeobject (object key) {
        Object result = null;
        Jedisconnection jedisconnection = null;

            try {//Establish connection Jedisconnection = (jedisconnection) jedisconnectionfactory.getconnection ();

            redisserializer<object> serializer= new Jdkserializationredisserializer (); Remove saved to value Resul
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.