Hashmap in php application context

I am trying to implement hashmap (associative array in PHP) in PHP, which is available for widespread use, and save it in the application context, it should not be lost when the program ends. How can I do this in PHP?

Thank,

+5
source share
2 answers

If you are using the Zend version of php, this is easy.
You do not need to serialize the data.
Only content can be cached. Resources, such as file descriptors, cannot. To keep true / false, use 1.0 so that you can distinguish the cache from the result with ===.

Store:

zend_shm_cache_store('cache_namespace::this_cache_name',$any_variable,$expire_in_seconds);

Download:

$any_variable = zend_shm_cache_fetch('cache_namespace::this_cache_name');

if ( $any_variable === false ) {
    # cache was expired or did not exist.
}

For long-term data, you can use:

zend_disk_cache_store();zend_disk_cache_fetch();

For those who do not have zend, the corresponding APC versions above are:

Store:

apc_store('cache_name',$any_variable,$expire_in_seconds);

Download:

$any_variable = apc_fetch('cache_name');

if ( $any_variable === false ) {
    # cache was expired or did not exist.
}

. , /unserialize . , , zend , concurrency :

Store:

file_put_contents('/tmp/some_filename',serialize($any_variable));

:

$any_variable = unserialize(file_get_contents('/tmp/some_filename') );

: concurrency , , - . psuedo , .

Psuedo:

while ( lock exists ) {
    microsleep;
}
get lock.
check we got lock.
write value.
release lock.
+1

APC similars , , , .

Remember that this will not persist between server restarts, of course.

+1
source

All Articles