PHP uses APC to implement the mutex lock

Source: Internet
Author: User
Tags apc

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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.