Redis implementation (java)

Daily operations

public  static  void main (String [] args) 
    { 
        Jedis jedis = new new Jedis ( " 192.168.222.129 " , 6379 ); 
        
        // . 1, open transaction 
        the Transaction Transaction = jedis.multi (); 
        
        // 2, performed 
        . Transaction SET ( " K4 " , " V4 " ); 
        transaction. the SET ( " K5 " , " V5 " ); 
        
        // 3, commit the transaction
        transaction.exec (); 
        
        // . 4, transaction is aborted
         // transaction.discard (); 
    }

Locking Affairs

 public static void main(String[] args)
    {
        TestTx testTx = new TestTx();
        boolean retValue = testTx.transMethod();
        System.out.println("main=====return value " + retValue);
    }
    
    private boolean transMethod()
    {
        // TODO Auto-generated method stub
        Jedis jedis = new Jedis("192.168.222.129", 6379);
        int balance; //Available balance 
        int Debt Total; // Under - 
        int the Pay = 10 ; // actual bill amount 
        
        jedis.watch ( " Balance " ); 
        Balance = Integer.parseInt (jedis. GET ( " Balance " ));
         IF (Balance < the Pay) 
        { 
            jedis.unwatch (); 
            . System OUT .println ( " You are out of balance " );
             return  false ; 
        } 
        the else 
        {
            . SystemOUT .println ( " ============ ======== meeting consumer " );
             // 1, open transaction 
            Transaction Transaction = jedis.multi ();
             // 2, execution 
            transaction.decrBy ( " Balance " , the Pay); 
            transaction.incrBy ( " Debt Total " , the Pay);
             // 3, the transaction is committed 
            transaction.exec (); 
            Balance = Integer.parseInt (jedis. GET ( " Balance " )); 
            Debt Total = Integer.parseInt (jedis. GET("debt"));
            
            System.out.println("余额: " + balance);
            System.err.println("花费: " + debt);
            return true;
        }
    }

result

======== ============ meeting consumer 
Balance: 90 
main ===== return value to true 
cost: 10

 

Guess you like

Origin www.cnblogs.com/karrya/p/11282963.html