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
  • 阅读 ( 638 )
  • 分类:其他类型

你可能感兴趣的文章

相关问题

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 问答工具