laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 31 forks source link

[Feature] Enable flush method from dynamoDB #2653

Open didix16 opened 3 years ago

didix16 commented 3 years ago

Hi everyone,

I'm using Laravel Vapor for a serverless project and also I'm using DynamoDB as a caching system. I just realized that flush method is throwing and Exception:

/**
     * Remove all items from the cache.
     *
     * @return bool
     *
     * @throws \RuntimeException
     */
    public function flush()
    {
        throw new RuntimeException('DynamoDb does not support flushing an entire table. Please create a new table.');
    }

I don't understand why because it can be implemented by using the DynamoDbClient::scan method and then delete all items looping through them or just deleting the table and recreating it again.

Here is my approach:


 // @method \Aws\Result scan(array $args = []) <== This is from DynamoDBClient class
/**
     * Remove all items from the cache.
     *
     * @return bool
     */
    public function flush()
    {
        // This returns the entire items list on dynamoDB table
        $items = $this->dynamo->scan([
            'TableName' => $this->table,
            ...what ever the options this API call needs ....
        ]);
        $result = true;

        // I dont know how the scan method result  and item structure are but suppose it has a getKey() method to get the key....
        foreach($items as $item){
           $result =  $this->forget($item->getKey()) && $result;
        }
        // if all items could be deleted then flush will return true, else false.
        return $result;
    }

So what do you think? Hope could be implemented.

Thanks in advance.