Redis clients Jedis

1, Redis supports subscription and publishing messages

Redis news subscription support: The first subscription release

Subscribe: the Subscribe c1 c2

Published: publish the Hello-c2 Redis

Support wildcard subscription: psubscribe new new *

The actual development useless, RabbitMQ

2, Redis for transaction support

Redis is also support for the transaction, but the support is not very good.

multi: open affairs

exec: commit the transaction

discard: roll back the transaction

3, Redis of Java client Jedis

3.1 to introducing dependencies

 

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

 

3.2, the test code

 public  static  void main (String [] args) {
         // 1. Create Jedis client and server Redis designated ip and port number 
        Jedis jedis = new new Jedis ( "192.168.58.10", 6379 );
         // 2. By Jedis The client server operating Redis 
        jedis.set ( "key1", "VALUE1" ); 
        String VALUE1 = jedis.get ( "key1" ); 
        System.out.println (VALUE1); 
        // 3. Close Jedis client 
        jedis.close (); 
    } 
this one also needs to create! ! not too good

3.3, create jedisPool

This needs to be created with each use a connection pool consumes memory   

public static void main (String [] args) { // 1. Create JedisPool object acquired from the connection Jedis JedisPool object JedisPool jedisPool = new new JedisPool ( "192.168.58.10" , 6379 ); Jedis jedis = jedisPool.getResource (); // 2. Jedis by the client server operating Redis String VALUE1 = jedis.get ( "key1" ); System.out.println (VALUE1); // 3. Close Jedis client jedis.close (); }

 

Four Jedis and Spring integration,

4.1 Import and spring package jedis

4.2: Configuration File: This would not create a time would not only create jedis pool on the line once used.

<? XML Version = "1.0" encoding = "UTF-. 8"?> 
<Beans xmlns = "http://www.springframework.org/schema/beans" 
       xmlns: the xsi = "http://www.w3.org / 2001 / the XMLSchema-instance " 
       the xsi: the schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> 

    <= the bean ID "jedisPoolConfig" class = "redis.clients.jedis.JedisPoolConfig"> 
        <-! maximum number of idle connections -> 
        <Property name = "maxIdle" value = ". 1" /> 
        <-! maximum number of connections -> 
        <Property name = "maxTotal" value = "5" /> 
        <-! when the connection is blocked exhausted, false report abnormal,ture blocked until a timeout, default to true -> 
        <Property name = "blockWhenExhausted" value = "to true" />
        <! - get the maximum number of milliseconds to wait for a connection (if set to blocking BlockWhenExhausted), if the timeout to throw exceptions, less than zero: blocking indefinite time, default -1 -> 
        <Property name = "maxWaitMillis" value = "30000" /> 
        <! - get checked for validity at the time of connection -> 
        <Property name = "testOnBorrow" value = "to true" /> 
    </ bean> 

    <bean class = "redis.clients.jedis. JedisPool "the destroy-Method =" Close "> 
        <constructor Arg-name =" Host "value =" 192.168.248.99 "/> 
        <constructor Arg-name =" Port "value =" 6379 "/> 
        <name = Arg-constructor "poolConfig" REF = "jedisPoolConfig" /> 
    </ the bean> 

</ Beans>

Five, Jedis and integration of SSM

5.1: Lead Pack

5.2: Configuration and integration ssm jedis profile

5.2: Then redis cache can only be applied in the service layer.

The first

This tool is the use of a custom class is converted into a string array, the array turned into a string of cache ways.

Create custom serialization, deserialization Tools

 

Package com.ujy.maven.utils;
 Import the java.io. * ;
 public  class SerizlizableUtil { 

    // complete sequence of 
    public   static  byte [] Serializable (Object Object) {
       / * 
      * a receiving object writeObject spread inside, through the object output stream of bytes written to the output stream 
      *, and then obtain from the array toByteArray byte array output stream, and then returns to the caller 
      * * / 
       the try {
            // 1. ByteArrayOutputStream 
           ByteArrayOutputStream BAOS = new new ByteArrayOutputStream () ; // 3
            // 2 target output flow 
           the ObjectOutputStream OOS = new newThe ObjectOutputStream (BAOS); // 2 
           oos.writeObject (Object);   // . 1 
           byte [] bytes = baos.toByteArray (); // . 4 
           return   bytes; // . 5 
       } the catch (Exception E) { 
           e.printStackTrace () ; 
       } 
       return   null ; 
    } 
    // deserialization is complete 
    public  static Object unSerializable ( byte [] bytes) {
    // first determine whether the non-empty non-empty direct return 
      iF (bytes == null ) {
           return   null ; 
      }
        the try {
         // 1. byte array input stream 
        A ByteArrayInputStream Bais = new new A ByteArrayInputStream (bytes);
         // 2. object input stream 
         the ObjectInputStream OIS = new new the ObjectInputStream (Bais); 
            Object O = ois.readObject ();
             return    O; 
        } the catch (Exception E) { 
            e.printStackTrace (); 
        } 
        return   null ; 
    } 
}

 

Then in the service layer applications:

@Autowired
     Private the PersonDao PersonDao; 
    @Autowired 
    Private JedisPool jedisPool; 

 public List <the Person> the findAll () { 
        Jedis jedis = jedisPool.getResource (); 
        List List = <the Person> null ; 

        Long the startTime = System.currentTimeMillis (); 
        the System.out .println (the startTime); 
        // 1. go to check data cache, 
        byte [] = jedis.get Lists (SerizlizableUtil.serializable ( "personList" ));
         // 2. If it is not stored in the database and from the investigation cache next time you can take from the cache. 
        IF (Lists == null ) { 
             List= PersonDao.findAll ();
             Long endTime1 = System.currentTimeMillis (); 
            System.out.println ( "database query to the data used in time:" + (endTime1- the startTime)); 
            jedis.set (SerizlizableUtil.serializable ( "personList" ), SerizlizableUtil.serializable (List)); 
         } the else {
             // 3. If there is data in the cache, direct deserialization, the control returns to the layer 
             List = (List <the Person> ) SerizlizableUtil.unSerializable (Lists); 
            System.out.println (List); 
            Long endTime2 = System.currentTimeMillis (); 
            System.out.println ( "query data to a cache of time:" + (endTime2-startTime));
        }
          jedis.close();
         return  list;
    }

 

 

The second:

This is defined using tools such FastJson, is converted to a string, then the string into the object or set or map.

First to two test code: this is converted into any type String.

 

  @Test
     public  void test1 () {
         // the object ---> String 
      the Person Person = new new the Person (. 6, "Prince", 1,22 ); 
        String S = JSON.toJSONString (Person); 
        System.out.println ( S); 

        // object list (set) ---> string 
        list <the Person> list = new new the ArrayList <the Person> (); 
        List.add ( new new the Person (. 7, "princess", 0,20 )); 
        list .add ( new new the Person (. 8, "princess", 0,21 )); 
        List.add ( new new the Person (. 9, "Happy", 1,23 )); 
        String S1 =JSON.toJSONString (List); 
        System.out.println (S1); 

        // the Map ---> string 
        the Map <String, Object> = Map new new the HashMap <String, Object> (); 
        map.put ( "Li Flying "," knife without false hair " ); 
        map.put ( " feiyanzoubi "," is also real " ); 
        String S = JSON.toJSONString (Map); 
        System.out.println (S); 

    }

 

This is converted into String objects, List, Array, Map

Regardless of any data type into a string through it: JSON.toJSONString () 
     string into other data types: 
          String ---> object type JSON.parseObj () 
           String ---> array type JSON.parseArray () 
           String - -> the Map type JSON.parseObj () 




@Test 
    public  void test2 () {
        // String ---> object type JSON.parseObj () 
        the Person Person = new new the Person (. 6, "Prince", 1,22 ); 
        String S = JSON.toJSONString (Person); 

        the Person PERSON1 = JSON.parseObject (S, the Person. class ); 
        System.out.println (PERSON1); 

        //String--->数组类型JSON.parseArray()
     List<Person>  list = new ArrayList<Person>();
        list.add(new Person(7,"格格",0,20));
        list.add(new Person(8 ,"公主",0,21));
        list.add(new Person(9,"逍遥",1,23));
        String s1 = JSON.toJSONString(list);

        List<Person> people = JSON.parseArray(s1, Person.class);
        System.out.println(people);

        //String --->Map类型JSON.parseObj()
        Map<String,Object> map = new HashMap<String, Object>();
        map.put ( "Li flying", "knife without false hair" ); 
        map.put ( "feiyanzoubi", "is also real" ); 
        String S = JSON.toJSONString (Map); 

        the Map MAP1 = JSON.parseObject (. S, the Map class ); 
        System.out.println (MAP1); 
    }

Then applied to the service layer

  @Autowired
     Private the PersonDao PersonDao; 
    @Autowired 
    Private JedisPool jedisPool; 


 public List <the Person> the findAll () { 
        Jedis jedis = jedisPool.getResource (); 
        List List = <the Person> null ; 

        Long the startTime = System.currentTimeMillis (); 
        the System.out .println (the startTime); 
        // 1. go to check data cache, 
        String sss = jedis.get ( "personList" );
         // 2. if not then the next from the database stored in the buffer from the cache check can take . 
        IF (sss == null ) { 
            List =personDao.findAll ();
             Long endTime1 = System.currentTimeMillis (); 
            System.out.println ( "database query to the data used in time:" + (endTime1- the startTime)); 
            jedis.set (JSON.toJSONString ( " personList " ), JSON.toJSONString (List)); 
        } the else {
             // 3. If there is data in the cache, direct deserialization, the control returns to the layer 
            List = JSON.parseArray (sss, the Person. class ); 
            the System.out .println (List); 
            Long endTime2 = System.currentTimeMillis (); 
            System.out.println ( "query to the cache data for the time:" + (endTime2- the startTime)); 
        }
        jedis.close();
        return  list;
    }

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/ych961107/p/11931470.html