javaはRedisデータベースに対して操作するDBHelper類を編纂します。

11308 ワード

JAVA言語でRedisデータベース操作に対するDBHelper類を作成します。その中で、主にデータベースの添削に対して、この調査方法は単一と集合の種類に分けられます。プログラムは二つの部分に分けられています。テスト方法とパッケージ方法です。
import redis.clients.jedis.Jedis;
/**
 *redis DBHelper  
 *
 */
public class DBHelper {
    //    
    private Jedis jedis = null;//      
    //     
    public static void main(String[] args) {
        //    
        String LOCALHOST = "localhost";
        int LOCALPOST = 6379;
        String key = "key001";
        String value = "abc";
        // String key2="key002";
        String value2 = "aaa";
//      String keysValues = "'key201','value201','key202','value202'";
//      String keys = "'key201','key202'";
//      String[] keys2 = { "key201", "key202" };

        //        
        DBHelper test = new DBHelper();
        if (test.init(LOCALHOST, LOCALPOST)) {
            test.set(key, value);
            test.get(key);
            test.updata(key, value2);
//          test.delete(key);

//          test.setMap(keysValues);
//          test.getMap(keys);
//          test.delMap(keys2);
//          test.flush();
            test.uninit();
        } else {
            System.out.println("      ,      !");
        }
    }
    /**
     *      
     * @param LOCALHOST:    
     * @param LOCALPOST:      :  6379.
     * @return
     */
    public boolean init(String LOCALHOST, int LOCALPOST) {
        if (jedis == null) {
            jedis = new Jedis(LOCALHOST, LOCALPOST);
            return true;
        } else {
            return false;
        }
    }
    /**
     *     ,    
     */
    public void uninit() {
        System.out.println("    redis     。");
    }
    /**
     *       
     * @param key:  
     */
    public boolean exist(String key) {
        if (jedis != null) {
            return jedis.exists(key);
        } else {
            return false;
        }
    }
    /**
     *     
     * @param key:  
     * @param value:   
     */
    public String set(String key, String value) {
        if (jedis != null) {
            boolean exist = exist(key);
            if (exist == false) {
                jedis.set(key, value);
                return "         。";
            } else {
                return "          。";
            }
        } else {
            return "        ,    ";
        }
    }
    /**
     *     
     * @param key:  
     */
    public String get(String key) {
        if (jedis != null) {
            boolean exist = exist(key);
            if (exist == true) {
                jedis.get(key);
                System.out.println("         :" + jedis.get(key));
                return "       。";
            } else {
                return "       ,     ";
            }
        } else {
            return "        ,    ";
        }
    }
    /**
     *     
     * @param key:  
     * @param value:   ,          ,         
     */
    public String updata(String key, String value) {
        if (jedis != null) {
            boolean exist = exist(key);
            if (exist == true) {
                jedis.set(key, value);
                return "         。";
                // jedis.append(key, value);
                // return "          ";
            } else {
                jedis.set(key, value);
                return "     ,         。";
            }
        } else {
            return "        ,    ";
        }
    }
    /**
     *     
     * @param key:  
     */
    public String delete(String key) {
        if (jedis != null) {
            boolean exist = exist(key);
            if (exist == true) {
                jedis.del(key);
                return "       。";
            } else {
                return "    ,    。";
            }
        } else {
            return "        ,    ";
        }
    }
    /**
     *       
     * @param keysvalues:    
     */
    public String setMap(String keysvalues) {
        if (jedis != null) {
            jedis.mset(keysvalues);
            return "         。";
        } else {
            return "        ,    ";
        }
    }
    /**
     *       
     * @param keys:    
     */
    public String getMap(String keys) {
        if (jedis != null) {
            jedis.mget(keys);
            return "         。";
        } else {
            return "        ,    ";
        }
    }
    /**
     *       
     * @param keys:    
     */
    public String delMap(String[] keys) {
        if (jedis != null) {
            jedis.del(keys);
            return "         。";
        } else {
            return "        ,    ";
        }
    }
    /**
     *        
     */
    public String flush() {
        if (jedis != null) {
            jedis.flushDB();
            return "        !";
        } else {
            return "     ,    。";
        }
    }
}