Friends who have used the SAE platform should know that due to platform security considerations, SAE limits users' use of local IO. But this may bring a lot of inconvenience to some traditional PHP projects, because they all have more or less local IO operations, such as Smarty's compilation template. To solve this problem, SAE provides the TmpFS function. TmpFS allows developers to temporarily read and write local IO through standard IO functions, which facilitates the porting of many non-SAE projects.
But TmpFS is not enough. Judging from the name, it is a temporary file system. Its life cycle is the same as that of a PHP request. That is, when the PHP request completes execution, all temporary files written to TmpFS will be destroyed. . TmpFS is a local temporary file, not a shared storage, and SAE is a fully distributed environment, so different requests cannot share operation files through TmpFS. For details, please refer to the SAE platform documentation.
Example: For example, I want to use TmpFS to make a counter (of course you can also use the counter service provided by SAE)
The code is as follows:
<strong><span 1</span> <?<span php </span><span 2</span> <span $file</span>=SAE_TMP_PATH."/test.txt"<span ; </span><span 3</span> <span if</span>(!<span file_exists</span>(<span $file</span><span )){ </span><span 4</span> <span file_put_contents</span>(<span $file</span>,1<span ); </span><span 5</span> <span echo</span> 1<span ; </span><span 6</span> }<span else</span><span { </span><span 7</span> <span $n</span>=<span file_get_contents</span>(<span $file</span><span ); </span><span 8</span> <span $n</span>++<span ; </span><span 9</span> <span echo</span> <span $n</span><span ; </span><span 10</span> <span file_put_contents</span>(<span $file</span>,<span $n</span><span ); </span><span 11</span> <span 12</span> <span } </span><span 13</span> <span 14</span> <span 15</span> ?></strong>
I found that it is impossible to execute in else, because the temporary file system no longer exists with the end of each execution, so every time the code starts, it is judged (it is a new php request), that The temporary file no longer exists.
That is to say, zero-time files cannot be shared between two files or different time requests for a file.
In fact, if you read the SAE documents carefully, it is not difficult to find that
is included in the Wrappers provided by SAE.KVDB -- saekv://
saekv:// is used to read and write KVDB. It is mainly used to save persistently stored data. The most commonly used scenario is to save configuration files
This can meet our requirements for creating and modifying persistent files
You must first before using this service.
The following is the test code for the counter:
<strong><span 1</span> <?<span php </span><span 2</span> <span $file</span>="saekv://count.txt"<span ; </span><span 3</span> <span if</span>(!<span file_exists</span>(<span $file</span><span )){ </span><span 4</span> <span file_put_contents</span>(<span $file</span>,1<span ); </span><span 5</span> <span echo</span> 1<span ; </span><span 6</span> }<span else</span><span { </span><span 7</span> <span 8</span> <span $n</span>=<span file_get_contents</span>(<span $file</span><span ); </span><span 9</span> <span echo</span> <span $n</span><span ; </span><span 10</span> <span $n</span>++<span ; </span><span 11</span> <span file_put_contents</span>(<span $file</span>,<span $n</span><span ); </span><span 12</span> <span 13</span> <span 14</span> }</strong>
That’s it.
There are two ways to delete files simultaneously
One way is to use PHP’s native deletion method unlink($file)
Another way is to delete the kvdb database provided by SAE:
Code:
<strong><span 1</span> <span $file</span>="saekv://count.txt"<span ; </span><span 2</span> <span $kv</span>=<span new</span><span SaeKV(); </span><span 3</span> <span $kv</span>-><span init(); </span><span 4</span> <span if</span>(<span $kv</span>->delete("count.txt"<span )){ </span><span 5</span> <span echo</span> "ok"<span ; </span><span 6</span> }<span else</span><span { </span><span 7</span> <span echo</span> "no"<span ; </span><span 8</span> }</strong>
For details, please refer to the KVDB service document. count.txt is the key value...
Reference documentation: SAE platform documentation