• <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>

    redis數據庫的java接入,redisson的使用

    redis是一個很是高效的內存型數據庫,數據存取速度甩mysql等硬盤數據庫幾條街

    東西/原料

    • java開辟情況
    • 收集

    方式/步調

    1. 1

      起首可以百度redis看一下官網領會一下,不外redis的官網對于此次經驗來說并沒有太年夜的用處

    2. 2

      redis采用c說話編寫,所以用在java上并不是直接利用的,而是去接入一個叫做redisson的項目,這個項目可以在github上找到

    3. 3

      從redisson項目中下載我們需要的jar包,別的需要聯系關系源碼的小伙伴可以趁便把源碼下載一下,打當作zip包放在eclipse中進行源碼聯系關系,便利追源碼

    4. 4

      在redis官網上找到windows版本的service安裝包,位置是在這里,然后會導航到由微軟維護的redis windows項目中,找到realease頁面,然后鄙人這個不變版的zip包

    5. 5

      然后這里就可以打開redis的當地service辦事了,就像mysql之類的數據庫一樣

    6. 6

      為了利用加倍便利,我們可以去下載一個redis的可視化東西,下載安裝之后,毗連到當地的service即可看到所稀有據庫表

    7. 7

      這是測試所用到的類:

      package db;

      import java.util.Queue;  

      import java.util.Set;  

      import java.util.concurrent.ConcurrentMap;  

        

      import org.redisson.Redisson;

      import org.redisson.config.Config;  

      public class RedisExample {  

        

          /** 

           * @param args 

           */  

          public static void main(String[] args) {  

              // 1.初始化設置裝備擺設

              Config config = new Config();  

              config.useSingleServer().setAddress("http://127.0.0.1:6379");  //要毗連的redis庫

              Redisson redisson = (Redisson) Redisson.create(config);  //建立毗連

              System.out.println("reids毗連當作功...");  

        

              // 2.測試concurrentMap,put方式的時辰就會同步到redis中 ,在redis中建立了一張FirstMap表

              ConcurrentMap<String, Object> map = redisson.getMap("FirstMap");  

              map.put("wuguowei", "男");  

              map.put("zhangsan", "nan");  

              map.put("lisi", "女");  

              

              ConcurrentMap resultMap = redisson.getMap("FirstMap");  

              System.out.println("resultMap==" + resultMap.keySet());  

        

              // 2.測試Set調集 ,建立MySet的set調集表

              Set mySet = redisson.getSet("MySet");  

              mySet.add("wuguowei");  

              mySet.add("lisi");  

        

              Set resultSet = redisson.getSet("MySet");  

              System.out.println("resultSet===" + resultSet.size());  

                

              //3.測試Queue隊列  

              Queue myQueue = redisson.getQueue("FirstQueue");  

              myQueue.add("wuguowei");  

              myQueue.add("lili");  

              myQueue.add("zhangsan");  

              

              Queue resultQueue=redisson.getQueue("FirstQueue");  

              System.out.println("resultQueue==="+resultQueue);  

                

              // 封閉毗連  

              redisson.shutdown();  

          }  

        

      }  

    8. 8

      運行之后會發現當作功鏈接到數據庫,不外出了一些小問題,本家兒要日記包反復,這里是因為我們的redis是和webmagic連系利用的,兩個都有slf4j的日記包的緣故,這里我選擇刪除redis的jar中的slf4j包,我們用winrar打開redis.jar,然后找到里面的slf4j目次并刪除,這時獲得一個zip文件,然后再將zip文件的后綴名改為.jar即可

    9. 9

      ps,于2017/09/07彌補:

      這是我本身寫的一個簡單操作redis數據庫的Dao類,可以作為參考

      package db;

      import java.util.Set;  

        

      import org.redisson.Redisson;

      import org.redisson.config.Config;  

      public class RedisDao {  

      public static int STATUS = 0;

      private static Redisson redisson;

      static {

      //啟動redis辦事器

      openExe();

      // 1.初始化設置裝備擺設

              Config config = new Config();  

              config.useSingleServer().setAddress("http://127.0.0.1:6379");  //要毗連的redis庫

              redisson = (Redisson) Redisson.create(config);  //建立毗連

              System.out.println("reids毗連當作功...");  

      }

      public static void add(String setName,String url) {

      // 將url插手set表中

              Set mySet = redisson.getSet(setName);  

              mySet.add(url);  

      }

      public static boolean querySet(String setName,String url) {

      // 查詢set中是否包含url

              Set mySet = redisson.getSet(setName);  

              return mySet.contains(url);

      }

      public static void closeRedis() {

      // 封閉毗連  

              redisson.shutdown();  

      }

      // 挪用可執行文件 

      public static void openExe() {  

         final Runtime runtime = Runtime.getRuntime();  

         Process process = null; 

         try {  

          //redis-server.exe的路徑

             process = runtime.exec("G:\\eclipse4.7space\\Redis-x64-3.0.504\\redis-server.exe");  

             STATUS = 1;

         } catch (final Exception e) {  

             System.out.println("Error exec!");  

         }  

      }       

      }  

    10. 10

      別的需要注重一下redis的默認持久化法則,單key操作完當作后15min后保留,10key操作完當作5min后保留,10000key操作完當作后,1min后保留,且以上保留只發生在操作完當作之后,持續操作間斷電將導致數據丟掉

      # Save the DB on disk:

      #

      #   save <seconds> <changes>

      #

      #   Will save the DB if both the given number of seconds and the given

      #   number of write operations against the DB occurred.

      #

      #   In the example below the behaviour will be to save:

      #   after 900 sec (15 min) if at least 1 key changed

      #   after 300 sec (5 min) if at least 10 keys changed

      #   after 60 sec if at least 10000 keys changed

      #

      #   Note: you can disable saving completely by commenting out all "save" lines.

      #

      #   It is also possible to remove all the previously configured save

      #   points by adding a save directive with a single empty string argument

      #   like in the following example:

      #

      #   save ""

      save 900 1

      save 300 10

      save 60 10000

    注重事項

    • 若是感覺有效,請封閉告白屏障插件,多瀏覽我幾篇經驗哦,一個有用瀏覽有2分錢哈
    • 發表于 2018-03-13 00:00
    • 閱讀 ( 609 )
    • 分類:其他類型

    你可能感興趣的文章

    相關問題

    0 條評論

    請先 登錄 后評論
    admin
    admin

    0 篇文章

    作家榜 ?

    1. xiaonan123 189 文章
    2. 湯依妹兒 97 文章
    3. luogf229 46 文章
    4. jy02406749 45 文章
    5. 小凡 34 文章
    6. Daisy萌 32 文章
    7. 我的QQ3117863681 24 文章
    8. 華志健 23 文章

    聯系我們:uytrv@hotmail.com 問答工具
  • <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>
    久久久久精品国产麻豆