2

I'm currently using Shrine to upload files to DigitalOcean (via s3 sdk). It works nicely, it's perfect. However, in the guide there's a storage option to make a temporary copy of the uploads, specified by the 'cache' prefix.

How is this cache used? Or, put differently, what features does it provide?

Since I'm totally unaware of its use, currently all I'm seeing are duplicates of my uploads in my Spaces (bucket) resource. Are these files ever disposed of?

Finally, if the cached files are for data retrieval purposes, wouldn't it make sense to make a local cache, rather than it being sent to the S3 resource?

I apologize if all of this is general knowledge, it didn't seem clear to me as I was rushing in to get it implemented.

dev404
  • 1,088
  • 13
  • 34

2 Answers2

4

Shrine's temporary storage is used mainly to prevent orphan files (files not attached to any record) from entering your primary storage. An uploaded file might not end up attached in case of validation errors, or if the user decides not to save the form after the file has been asynchronously uploaded to the storage.

Because Shrine's uploaded files are not backed by database records by default (like with Active Storage), the temporary storage also provides a security measure where it prevents users from hijacking files of other users. If only the primary storage were used, an attacker could guess the uploaded file ID from the URL of another file, and assign it in their form when creating a record. Afterwards they could delete the record, and the file belonging to the other user would get deleted with it.

Shrine recommends using cloud storage for temporary storage to enable direct uploads to the cloud storage from the browser, and also because disk storage doesn't work if you're hosting your app on multiple servers, since only one server would have access to the saved file. Note that you can still use disk for temporary storage if you want to, just change the :cache declaration.

Janko
  • 8,985
  • 7
  • 34
  • 51
2

Shrine used cache to move slow processing action on background. You can specify some fast actions on caching and then make heavy processing in background. This is improving user side effect of uploading files. Also Shrine does not delete temporary files and you need to destroy it yourself in background

Vasiliy Novikov
  • 341
  • 1
  • 7