Help Center> GeminiDB> GeminiDB Redis API> Best Practices> EXHASH Solution for Ad Frequency Control
Updated on 2024-05-20 GMT+08:00

EXHASH Solution for Ad Frequency Control

EXHASH, a hash data type, provides general HASH functions and allows you to specify expiration times and version numbers for fields. It supports flexible data structures and can help you simplify service development in various scenarios.

This section describes how to use EXHASH commands of GeminiDB Redis API to simplify service development of frequency control and shopping cart.

EXHASH Commands

For details, see EXHASH commands.

Scenarios

  • Frequency control

    Frequency control allows users to restrict the number of operations performed within a certain period (for example, one day, one week, or one month), and limit the number of times an ad or information displayed on a platform within a specified period. This helps prevent overexposure and ad fatigue, optimizes ad performance, improves the conversion rate, and avoid malicious activities, such as manipulating online traffic, comments and likes.

    Frequency control involves three elements: user ID (key), ad ID (field), and the number of times an activity is triggered (value). You can configure frequency control policies for an ad by referring the figure below.

    Figure 1 HASH solution
    • HASH solution 1 makes it easy to implement frequency control. You can use the EXPIRE command to set the expiration time of User_1 to one day and the HINCRBY command to increase how many times an ad is pushed. Before each ad push, you can use HGET to obtain this number. Data is retained for one day and then is automatically deleted. However, you can set only one expiration time for each user (each key). Flexible frequency control policies cannot be set for a specified period, for example, three pushes within eight hours.
    • HASH solution 2 allows you to splice timestamps into the value and use the HASH command to implement flexible frequency control. However, it increases the workload of service development.
    • The EXHASH solution allows you to set the expiration time for fields. In the frequency control scenario, GeminiDB Redis API allows you to configure a different push frequency for each ad and in different time segments. Assume that the frequency control policy configured for AD_2 is twice within 8 hours. Before pushing AD_2 to User_1, you can obtain the value 2 by running the EXHGET command, AD_2 will not be pushed to User_1 any more. After eight hours, the field for User_1 expires and the field information cannot be obtained by EXHGET. In this case, AD_2 will be pushed to User_1 again.
  • Shopping cart

    The following describes and compares several Redis solutions in the shopping cart scenario.

    1. STRING solution

      The shopping cart works easily with STRING commands. The platform combines the user ID and item ID as a key, for example, User_1#Earphones_1. The key value is the number of items to be purchased. There is an expiration time for items on flash sales.

      Figure 2 STRING solution
      • Related commands
        incrby User_N#Product_N [Number] #Increases the product quantity.
        set User_N#Product_N [Number] #Sets the product quantity.
        expire User_N#Product_N Time_N # Sets the expiration time of a specified item in the shopping cart of a specified user.
        get User_N#Product_N #Obtains the product quantity.
        scan 0 match User_N* # Queries all items of User_N.
        del User_N#Product_N #Deletes a specified item from the shopping cart of a specified user.
      • Possible issues are as follows:
        • Extra splicing increases the encoding and decoding development workload.
        • When a user requests to obtain the shopping item list, the SCAN command prefix is used to scan all keys and the GET command is used to obtain the corresponding value.
        • To obtain the list length, the number of prefix keys should be scanned.
        • There are a large number of duplicate username prefixes occupying the storage space.
    2. HASH solution

      This solution uses the user ID as the key and the item ID as the field. The value indicates the number of the item in the shopping cart. For items in flash sales, the expiration time is combined to the value of the field.

      Figure 3 HASH solution
      • Related commands
        hset User_N Product_N [Number#Time_N] # Sets the quantity and expiration time of a specified item in the shopping cart of a specified user.
        hincrby User_N Product_N [Number] # Adds the number of a specified item to the shopping cart of a specified user.
        hgetUser_N Product_N # Obtains information about a specified item in the shopping cart of a specified user.
        hgetall User_N #Obtains all item information of a specified user.
        hlen User_N # Obtains the number of items in the shopping cart of a specified user.
        hdel User_N Product_N #Deletes a specified item from the shopping cart of a specified user.
      • This solution is better than the STRING solution in the following ways:
        • Only a HGETALL command is required to obtain the shopping cart list of a user.
        • The HLEN command can be used to obtain the item list length of a user.
        • There are few duplicate username prefixes.

      However, the solution is complex for processing items in flash sales. For example, If the quantity for Keyboard_1 of User_1 needs to be added instead of using the HINCRBY command directly, you should obtain the value of Keyboard_1 by executing the HGET command first and decode the value. Then, specify the quantity to be added and encode the value using HSET.

    3. EXHASH solution

      Similar to the HASH solution, the user ID is used as the key, the item ID is used as the field, and the value is the number of the item in the shopping cart. This solution allows you to set an expiration time for items in flash sales by running the HSET command.

      Figure 4 EXHASH solution
      • Related commands
        exhset User_N Product_N ex Time_N # Sets the quantity and expiration time of a specified item in the shopping cart of a specified user.
        exhincrby User_N Product_N [Number] keepttl # Increases the number of specified items in the shopping cart of a specified user and retain the original expiration time. exhget User_N Product_N #Obtains information about specified items in the shopping cart of a specified user.
        exhgetall User_N #Obtains all item information of a specified user.
        exhlen User_N # Obtains the number of items in the shopping cart of a specified user.
        exhdel User_N Product_N #Deletes a specified item from the shopping cart of a specified user.
        del User_N #Empties the shopping cart of a specified user.
      • Compared with the HASH solution, this solution allows you to set the expiration time for each field. The EXHASH command makes it easy to use and reconstruct on the service side.

Code Example for Ad Frequency Control

import redis 
import datetime 
import os
def get_cur_time():
    return "[" + datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3] + "]"
def get_redis():
    """
This method is used to connect to a Redis instance.
    * host: Instance connection address.
    * port: Port of the instance. The default value is 6379.
    * password: Password for connecting to the instance.
    """
    # There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables.
    # In this example, the username and password are stored in the environment variables. Before running this example, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed.
    password = os.getenv('EXAMPLE_PASSWORD_ENV') 
    return redis.Redis(host='***', port=6379, password=password)
''Global frequency control policy. Display ad 1 for up to 2 times within three seconds and ad 2 for five times within five seconds.
frequency_stratege = {"ad_1" : [2, 3], "ad_2" : [5, 5]}
def push_ad_to_user(userId: str, adId: str):
    '''     This method is used to push a specified ad to a specified user.
    * userId: User ID.
    * adId: Ad ID.
    '''
    # If no frequency control policies are set for an ad, directly push the ad to the user.
    if adId not in frequency_stratege:
        print("no need control frequency, push ", adId, "to", userId)
        return True
    # Obtain how many times an ad is pushed for a user by user ID and ad ID.
    # Command usage: EXHGET key field.
    cnt = get_redis().execute_command("EXHGET " + userId + " " + adId)
    # If an ad has not been pushed to a user, directly push the ad to the user.
    if cnt == None:
        # Command usage: EXHINCRBY key field num [EX time].
        # Usage description: EXHINCRBY User ID Ad ID Push times (1) Expiration time of the ad
        cmd = "EXHINCRBY " + userId + " " + adId + " 1 EX " + str(frequency_stratege[adId][1])
        cur_cnt = get_redis().execute_command(cmd)
        print(get_cur_time(),"push", adId, "to", userId, "first time during", str(frequency_stratege[adId][1]), "seconds")
        return True
    # The result returned from Redis Python client is in bytes. Convert the result to a string and then to an integer.
    cnt = int(cnt.decode("utf-8"))
    if cnt < frequency_stratege[adId][0]:
        # Command usage: EXHINCRBY key field num KEEPTTL Retain the original expiration time of the field.
        cmd = "EXHINCRBY " + userId + " " + adId + " 1 KEEPTTL"
        cur_cnt = get_redis().execute_command(cmd)
        print(get_cur_time(), "push", adId, "to", userId, "current cnt:", cur_cnt)
        return True
    print(get_cur_time(), "Control frequency, can't push", adId, "to", userId, ", max cnt:", frequency_stratege[adId][0])
    return False
if __name__ == "__main__":
    for i in range(3):
        push_ad_to_user("usr_1", "ad_1")
    for i in range(6):
        push_ad_to_user("usr_1", "ad_2")
    for i in range(3):
        push_ad_to_user("usr_1", "ad_1")
    for i in range(12):
        push_ad_to_user("usr_1", "ad_2")

The script output is as follows:

The Python script executes slowly, and the expiration time of ad 2 is set to 5 seconds. Ad 2 can thus be successfully pushed to the user at December 15, 2023 07:09:56.530, 5 seconds after the first push time of December 15, 2023 07:09:51.349.

[2023-12-15 07:09:50.086] push ad_1 to usr_1 first time during 3 seconds
[2023-12-15 07:09:50.503] push ad_1 to usr_1 current cnt: 2
[2023-12-15 07:09:50.794] Control frequency, can't push ad_1 to usr_1 , max cnt: 2
[2023-12-15 07:09:51.349] push ad_2 to usr_1 first time during 5 seconds
[2023-12-15 07:09:51.745] push ad_2 to usr_1 current cnt: 2 
[2023-12-15 07:09:52.128] push ad_2 to usr_1 current cnt: 3 
[2023-12-15 07:09:52.889] push ad_2 to usr_1 current cnt: 4 
[2023-12-15 07:09:53.417] push ad_2 to usr_1 current cnt: 5 
[2023-12-15 07:09:53.632] Control frequency, can't push ad_2 to usr_1 , max cnt: 5
[2023-12-15 07:09:54.120] push ad_1 to usr_1 first time during 3 seconds 
[2023-12-15 07:09:54.769] push ad_1 to usr_1 current cnt: 2
[2023-12-15 07:09:54.915] Control frequency, can't push ad_1 to usr_1 , max cnt: 2 
[2023-12-15 07:09:55.211] Control frequency, can't push ad_2 to usr_1 , max cnt: 5
[2023-12-15 07:09:55.402] Control frequency, can't push ad_2 to usr_1 , max cnt: 5 
[2023-12-15 07:09:55.601] Control frequency, can't push ad_2 to usr_1 , max cnt: 5
[2023-12-15 07:09:55.888] Control frequency, can't push ad_2 to usr_1 , max cnt: 5 
[2023-12-15 07:09:56.087] Control frequency, can't push ad_2 to usr_1 , max cnt: 5
[2023-12-15 07:09:56.530] push ad_2 to usr_1 first time during 5 seconds 
[2023-12-15 07:09:57.133] push ad_2 to usr_1 current cnt: 2
[2023-12-15 07:09:57.648] push ad_2 to usr_1 current cnt: 3
[2023-12-15 07:09:58.107] push ad_2 to usr_1 current cnt: 4
[2023-12-15 07:09:58.623] push ad_2 to usr_1 current cnt: 5
[2023-12-15 07:09:58.865] Control frequency, can't push ad_2 to usr_1 , max cnt: 5 
[2023-12-15 07:09:59.096] Control frequency, can't push ad_2 to usr_1 , max cnt: 5

This section describes the features, usage, and application scenarios of the EXHASH command provided by GeminiDB Redis API. EXHASH provides a similar syntax to the native Redis HASH and the use is isolated from that of HASH. It allows you to specify expiration times and version numbers for fields. GeminiDB Redis API is dedicated to developing more easy-to-use enterprise-class features, helping customers implement easy O&M and efficiently develop services.