Redis has three main functions

In addition to the 5 basic data types we commonly use, Redis also provides some special functional modules. There are three types: Bitmaps, Hyperloglog and Geo.

bitmaps

  • An overview of the

    Bitmaps in Redis are not actual data types, but rather a set of bit-oriented operations defined on strings. Because strings are binary security BLOBs, and their maximum length is 512 MB, they are suitable for setting up to 2^32 different bits. One of the biggest advantages of bitmaps is that they often save a lot of space when storing information. For example, in a system where different users are represented by incremental user ids, it is possible to remember a single bit of information for 4 billion users with only 512MB of memory

  • Usage scenarios

    Active user statistics: Assume that a system has 1 million unique users logging in every day.

    Requirement 1: Collects statistics on the number of weekly active users.

    Requirement 2: Statistics on the number of users who log in every day of a week.

    Analysis, you need to store the login information of each user every day

  • Common commands

    Setbit Key offset Value Bitop Performs bit-by-bit operations between different strings. The operations provided are AND, OR, XOR, AND NOT. Bitcount performs the fill count, reporting the number of bits set to 1. Bitpos looks for the first bit with the specified value 0 or 1.Copy the code
  • code

    //TODO
    public class BitMapTest {
    
        public static void main(String[] args) {
            // Connect to the local Redis service
            Jedis jedis = new Jedis("localhost");
            // Initializes the check-in with ids 0-99
            IntStream.range(0.100).forEach((id) -> {
                        jedis.setbit("Week" + id % 7, id, true); });// the user whose id is 10 is logged in daily.
            for (int i = 0; i < 7; i++) {
                jedis.setbit("Week" + i , 10.true);
            }
    
            for (int i = 0; i < 7; i++) {
                System.out.println(String.format("The number of active users per week %s is %s",i, jedis.bitcount("Week" + i)));
            }
            jedis.bitop(BitOP.OR, "week"."Week 0"."Week 1"."Week 2"."Three weeks"."Week 4"."Friday"."Saturday");
            System.out.println(String.format("This week's active users are %s.",jedis.bitcount("week")));
    
            jedis.bitop(BitOP.AND, "week1"."Week 0"."Week 1"."Week 2"."Three weeks"."Week 4"."Friday"."Saturday");
            System.out.println(String.format("The number of users who logged in every day this week %s",jedis.bitcount("week1"))); }}Copy the code

  • conclusion

    Learning bitmap, we know that bitmap is a data structure that stores specific data through an array of bits. Each bit can contain information independently. Bit is the smallest storage unit of data, so it can save a lot of space. One of the obvious advantages of Bitmaps is that you can easily combine multiple statistical results by performing and, or, xOR, etc. You can also greatly reduce the amount of memory you need to do a simple calculation, if you want to count the cardinality of 100 million data, about the amount of memory you need.

    The data type Take up the space The number of users stored The amount of memory
    set 32 – 100000000 32 * 100000000/8 1024/1024/381 m material
    bitMap 1 a 100000000 100000000/8/1024/1024 material 12 m

hyperloglog

  • An overview of the

    What is the cardinal number? Let’s say I have a set of (1,4,2,7,8,7) and the cardinality of this set is the number of elements that have been decavened, which is 5.

    The memory savings of Bitmap are obvious, but not enough. It takes 12M to count the cardinality value of an object. If 10,000 objects are counted, nearly 120G is needed, which is also not widely used in big data scenarios.

    HyperLogLog implemented in Redis only needs 12K memory and can count 2^{64} data under the premise of standard error of 0.81%. HyperLogLog is a probabilistic algorithm. The probabilistic algorithm does not store the data set itself directly. The cardinality value is estimated by a certain probabilistic statistical method, which can greatly save memory and ensure that the error is controlled within a certain range.

  • Common commands

    pfadd key val1 val2 ... Pfcount Key Indicates the statistics baseCopy the code
  • Usage scenarios

    Statistics on the independent UV of a page. UV is the number of users who access a page independently every day. If a user accesses a page multiple times, the number is counted only once.

  • code

    public class HyperLogLogTest {
        public static void main(String[] args) {
            // Connect to the local Redis service
            Jedis jedis = new Jedis("localhost");
            // Initializes the check-in with ids 0-99
            IntStream.range(0.10000).forEach((id) -> {
                String userId = UUID.randomUUID().toString();
                jedis.pfadd("datetime:page1",userId); }); System.out.println("UV estimated by Hyperloglog probability algorithm:"+jedis.pfcount("datetime:page1")); }}Copy the code

  • conclusion

    Hyperloglog is a probability algorithm, is through the local calculation of the whole algorithm, in the case of no storage elements, with the cardinality of the set, but there is a certain error. If the error is within the business tolerance. So this is a very memory efficient algorithm.

geo

  • An overview of the

    In redis3.2, the calculation of Geo spatial location was added. Through GEO, we can calculate the distance between two geographic locations, and obtain the geographic location set within the specified range for a given geographic location

  • Common commands (geo is available in versions later than Redis3.2)

    (1) GEOPOS (1) GEOPOS (2) GEOPOS (3) GEODIST (4) GEORADIUS (4) GEORADIUSBYMEMBER: obtains the GEOHASH value of a given geographic location. GEORADIUSBYMEMBER: obtains the GEOHASH value of a given geographic location. GEORADIUSBYMEMBER: Obtains the GEOHASH value of a given geographic location

  • Usage scenarios

    Calculate the distance between the user and the merchant

  • code

    public class GeoTest {
        public static void main(String[] args) {
            // Connect to the local Redis service
            Jedis jedis = new Jedis("localhost");
            // Initialize the locations of the four stores
            Map<String, GeoCoordinate> map=new HashMap<>();
            map.put(Chenghua Xinfeng Road Franchise Store.new GeoCoordinate(104.11117.30.6846));
            map.put(Business Hall, Dongmen Street, Qingyang District.new GeoCoordinate(104.05983.30.66685));
            map.put(Business Hall, South Section 3, First Ring Road, Wuhou District.new GeoCoordinate(104.0614.30.63354));
            map.put("Jinniu District Sandongqiao Franchise Store".new GeoCoordinate(104.04903.30.67408));
            jedis.geoadd("shop",map);
            GeoRadiusParam param=GeoRadiusParam.geoRadiusParam()
                    .withDist()// Return the distance
                    .withCoord() // return latitude and longitude
                    .sortAscending();// Sort by ascending distance;
            double userLongitude=104.045181;// User's longitude
            double userLatitude=30.688663;// User's latitude
            // Query the locations within 10 km from the user
            List<GeoRadiusResponse> shop = jedis.georadius("shop", userLongitude, userLatitude, 10, GeoUnit.KM,param);
            for (GeoRadiusResponse geoRadiusResponse : shop) {
                System.out.println(String.format("User is %s km away from %s (latitude :%s, longitude :%s)", geoRadiusResponse.getMemberByString(), geoRadiusResponse.getCoordinate().getLongitude(), geoRadiusResponse.getCoordinate().getLatitude(), geoRadiusResponse.getDistance())); }}}Copy the code

  • conclusion

    Redis’ geo is passed by it only assuming that the Earth is a sphere, since the distance formula used is the Haversine formula. The formula is only an approximation when applied to the Earth, which is not a perfect sphere. Using Geo is a good choice in cases where it is not particularly accurate.