murilo's avatar
Level 10

best way to make log of upload images in Redis

hello , I have a upload of image with Laravel and VUE js ( A dynamic upload ) .

I need to make a log in Redis , to ensure that the user -

  • does not uploads more than 20 images in one hour
  • and does not uploads more that 200 images in one day .

I am thinking to create a new log every day for a user to verify those uploads . I am a little bit confused , what wold be the best way to do that and after search for those two options ( day and hour ).

I could store the user data with -

hmset -

Redis::hmset('user:1', [ '8:20' => '1.jpg', '9:21' => '2.jpg', '10:22' => '3.jpg', '10:27' => '7.jpg', '10:27' => '9.jpg', ]);

with hmset I could store inside the user.1 a Array with the time (hour and minutes) and those images that the user uploaded .

wold be possible to search on the last hour those images that is inside that array ? wold have a better option to use in Redis ? other better way to store that ?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To achieve the requirements of logging image uploads in Redis and ensuring that a user does not upload more than 20 images in one hour and not more than 200 images in one day, you can use Redis' sorted sets (zsets) to store timestamps of uploads. This will allow you to efficiently query and count uploads within specific time ranges.

Here's a step-by-step solution:

  1. Store Upload Timestamps in Redis:

    • Use a sorted set to store the timestamps of each upload. The score of each entry in the sorted set will be the timestamp of the upload.
  2. Check Upload Limits:

    • Before allowing an upload, check the number of uploads in the past hour and the past day using the ZCOUNT command.
  3. Add New Uploads:

    • After verifying the limits, add the new upload timestamp to the sorted set.
  4. Clean Up Old Entries:

    • Optionally, you can periodically clean up old entries to keep the Redis storage efficient.

Here is a code example in Laravel using the Redis facade:

use Illuminate\Support\Facades\Redis;

function canUpload($userId) {
    $currentTime = time();
    $oneHourAgo = $currentTime - 3600;
    $oneDayAgo = $currentTime - 86400;

    // Check uploads in the last hour
    $uploadsLastHour = Redis::zcount("user:{$userId}:uploads", $oneHourAgo, $currentTime);
    if ($uploadsLastHour >= 20) {
        return false; // Exceeded hourly limit
    }

    // Check uploads in the last day
    $uploadsLastDay = Redis::zcount("user:{$userId}:uploads", $oneDayAgo, $currentTime);
    if ($uploadsLastDay >= 200) {
        return false; // Exceeded daily limit
    }

    return true; // Allowed to upload
}

function logUpload($userId, $imageName) {
    $currentTime = time();

    // Add the upload timestamp to the sorted set
    Redis::zadd("user:{$userId}:uploads", $currentTime, $imageName);

    // Optionally, clean up old entries (older than one day)
    $oneDayAgo = $currentTime - 86400;
    Redis::zremrangebyscore("user:{$userId}:uploads", 0, $oneDayAgo);
}

// Example usage
$userId = 1;
$imageName = 'example.jpg';

if (canUpload($userId)) {
    logUpload($userId, $imageName);
    echo "Upload successful!";
} else {
    echo "Upload limit exceeded!";
}

Explanation:

  1. canUpload Function:

    • This function checks if the user can upload an image by counting the number of uploads in the last hour and the last day using zcount.
  2. logUpload Function:

    • This function logs the upload by adding the current timestamp and image name to the sorted set using zadd.
    • It also removes entries older than one day using zremrangebyscore to keep the sorted set clean.

Redis Key Structure:

  • user:{userId}:uploads: A sorted set where the score is the timestamp of the upload and the value is the image name.

This approach ensures that you can efficiently check and enforce the upload limits using Redis' sorted set operations.

Please or to participate in this conversation.