3

Assuming that I have an application running on Symfony 3.2 / Doctrine.

Question : What is the best way to store a single value that gonna change over some actions.

First thoughts : Im thinking that creating an Entity that is going to have a single property which will create a table with a single column that is going to have a single value (changing over some actions), is not an optimized solution.

I have also have thought about storing that value on a text file, but im saying that there might be another (better and more optimized) solution to this scenario.

Hint (if it can help) : My value is gonna be a generated PHP uniqid

Thank you in advance.

Youssef
  • 401
  • 1
  • 5
  • 23
  • 2
    Depends on the lifetime and who needs access to the value. Session or Cookie could be an option if the requirements for the persistence of the value are satisfied. More information on the exact use-case could help providing the best answer. – Joe Apr 28 '17 at 14:14
  • 2
    redis should be a good solution – Matteo Apr 28 '17 at 14:16
  • could you elaborate why you do not see files as good solutions to store a single value? just because it is not fancy enough is no valid argument. Each DB you use will store it in some kind of file in the background (except those which are memory only and risk loosing the value). would you decide the same if you would need to store an image instead of text? i am pretty sure in that case you would choose the file option without thinking about it – Norman M Apr 28 '17 at 14:19
  • File system (either as cache or via sqlite) could be viable. As the others said, depends on exactly your needs – kero Apr 28 '17 at 14:19
  • 1
    You need a key and a value, Redis is all about this. A file ? No, you can't open a file at each request that would no be optimized at all. – COil Apr 28 '17 at 14:51
  • 2
    Thank you all for your responses. For the last question, no I dont need a key / value system, but only a single value to store. Redis sounds like a good thing, but I dont see myself adding a huge dependency to the project for a single value to store. Any other suggestion ? – Youssef Apr 28 '17 at 15:28
  • 1
    Well a database is the solution for storing data. Text based storage was the thing before and it may be overkill, but the costs are so low that it doesn't matter. There is no reason to optimize for one single data you need to store. Just open the file with `w+` mode which puts the pointer to the start, reduces the file to 0 size and will write data. You also could use NoSQL for it. Should be something that is not a high dependency (even if it maybe is). The Textbased storage should be the most dependency free – Cagatay Ulubay Apr 28 '17 at 23:13
  • 1
    There is an interesting Symfony Cache component with multiple adapters (Filesystem, APCu, Redis...): http://symfony.com/doc/current/components/cache.html Symfony 3.3 will include a SimpleCache API, way more elegant: http://symfony.com/blog/new-in-symfony-3-3-simple-cache – Falc May 04 '17 at 10:36
  • @Falc thank you for this amazing answer. This is something like what I was looking for. – Youssef May 04 '17 at 13:02
  • 1
    Glad to help you. I'm writing it as an answer so you can tag it as "accepted". :) – Falc May 04 '17 at 14:14

1 Answers1

1

Symfony 3.1 included an interesting Cache component with multiple adapters (Filesystem, APCu, Redis...): http://symfony.com/doc/current/components/cache.html

As Symfony 3.2 the API seems to be confusing but Symfony 3.3 will include a SimpleCache API, way more elegant: http://symfony.com/blog/new-in-symfony-3-3-simple-cache

Falc
  • 595
  • 1
  • 5
  • 15