Redis Practice-Bitmaps

Source: Internet
Author: User

The main record is the use of Redis's setbit.

    • A string that we can save with Redis's setbit.
Import Org.junit.test;import Redis.clients.jedis.jedis;import Redis.clients.jedis.jedispool;import Redis.clients.jedis.jedispoolconfig;import Java.util.bitset;public class Bitmaptest {private static final String REDIS _path= "127.0.0.1"; IP address private static final int redis_port=6379;    Port number private static final String redis_auth= "iostream";    private static final Jedispool Jedispool;        static {//config Jedispool jedispoolconfig config=new jedispoolconfig ();        Config.setmaxtotal (10);        final int timeout=2000;    Jedispool=new Jedispool (Config,redis_path,redis_port,timeout,redis_auth); } private static int pos=0;    POS is the first bit that records the next character from which the first bit of the POS is 0, then the second character E's POS is 8, which accumulates up.        private static void Solve (String Binstr,jedis Jedis) {//Because we use integer.tobinarystring to omit the high of 0//So we need to make 0 first.        StringBuilder sb=new StringBuilder (); if (Binstr.length () <8) {for (int i=0;i<8-binstr.length (); ++i) {SB. append (0);        } sb.append (BINSTR);        } binstr=sb.tostring ();            for (int i=0;i<8;++i) {if (Binstr.charat (i) = = ' 0 ') {jedis.setbit ("test", pos+i,false);            }else {jedis.setbit ("test", pos+i,true);    }} pos+=8;        } public static void Main (string[] args) {Jedis jedis=jedispool.getresource ();        String s= "Hello"; try {for (int i = 0; i < s.length (); i++) {String binstr=integer.tobinarystring (S.charat (i)                  );                will take the ASCII encoding value and convert it to binary one char is two bytes (byte) 1byte=4bit a char has 8bit so the upper limit is 127.            The difference between byte and bit is to remember solve (Binstr,jedis);        } System.out.println (Jedis.get ("Test"));        }finally {jedis.close (); }    }}

Redis Practice-bitmaps

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.