Recently, we need to add a lock mechanism to the cache layer. Some Google Documents found that APC was not implemented (as if eaccelerator was used and never used). Only one lock is available, it is also relatively simple to implement with APC
Protected static function ismutex ($ key) {If (apccache: exists ("mutex _". $ key) return true; return false;} protected static function lock ($ key) {return apccache: add ("mutex _". $ key, true, 1);} protected static function unlock ($ key) {return apccache: Remove ("mutex _". $ key );}
The underlying layer has an encapsulation of apccache, and uses a one-second cache to test mutex.
Tested with a simple Python script, the implementation can meet the requirements
#! /Usr/bin/ENV Python #-*-coding: UTF-8-*-from multiprocessing import processimport urllibdef read (I): # print I, "In read" F = urllib. urlopen ("http: // help/index/testmulti? Param = % s "% I) Data = f. read () print dataif _ name _ = '_ main _': P = [] For I in range (300): p. append (process (target = read, argS = (I,) for C in P: C. start ()