Python編寫memcached啟動(dòng)腳本代碼實(shí)例
memcached是一套分布式的高速緩存系統(tǒng),由LiveJournal的Brad Fitzpatrick開(kāi)發(fā),但被許多網(wǎng)站使用。這是一套開(kāi)放源代碼軟件,以BSD license授權(quán)發(fā)布。
memcached缺乏認(rèn)證以及安全管制,這代表應(yīng)該將memcached服務(wù)器放置在防火墻后。
memcached的API使用三十二比特的循環(huán)冗余校驗(yàn)(CRC-32)計(jì)算鍵值后,將數(shù)據(jù)分散在不同的機(jī)器上。當(dāng)表格滿了以后,接下來(lái)新增的數(shù)據(jù)會(huì)以LRU機(jī)制替換掉。由于memcached通常只是當(dāng)作緩存系統(tǒng)使用,所以使用memcached的應(yīng)用程序在寫回較慢的系統(tǒng)時(shí)(像是后端的數(shù)據(jù)庫(kù))需要額外的代碼更新memcached內(nèi)的數(shù)據(jù)。
memcached作為緩存文件服務(wù),默認(rèn)是操作系統(tǒng)里面是可以直接yum -y install memcached進(jìn)行安裝的。
/etc/init.d/memcached 是屬于系統(tǒng)shell編寫的管理腳本,下面這個(gè)腳本是python腳本編寫出來(lái)的memcached管理腳本,和shell編寫的腳本實(shí)現(xiàn)的效果一樣。
代碼如下
#!/usr/bin/pythonimport sysimport osfrom subprocess import Popen,PIPEclass Process(object): ’’’memached rc script’’’ args = {’USER’:’memcached’, ’PORT’:11211, ’MAXCONN’:1024, ’CACHESIZE’:64, ’OPTION’:’’} def __init__(self,name,program,workdir): self.name = name self.program = program self.workdir = workdir def _init(self): ’’’/var/tmp/memcached’’’ if not os.path.exists(self.workdir): os.mkdir(self.workdir) os.chdir(self.workdir) def _pidFile(self): ’’’/var/tmp/memcached/memcached.pid’’’ return os.path.join(self.workdir, '%s.pid' % self.name) def _writePid(self): if self.pid: with open(self._pidFile(),’w’) as fd:fd.write(str(self.pid)) def _readConf(self,f): with open(f) as fd: lines = fd.readlines() return dict([ i.strip().replace(’'’,’’).split(’=’) for i in lines]) def _parseArgs(self): conf = self._readConf(’/etc/sysconfig/memcached’) if ’USER’ in conf: self.args[’USER’] = conf[’USER’] if ’PORT’ in conf: self.args[’PORT’] = conf[’PORT’] if ’MAXCONN’ in conf: self.args[’MAXCONN’] = conf[’MAXCONN’] if ’CACHESIZE’ in conf: self.args[’CACHESIZE’] = conf[’CACHESIZE’] options = [’-u’,self.args[’USER’], ’-p’,self.args[’PORT’], ’-m’,self.args[’CACHESIZE’], ’-c’,self.args[’MAXCONN’]] os.system('chown %s %s' % (self.args[’USER’],self.workdir)) return options def start(self): pid = self._getPid() if pid: print '%s is running...' % self.name sys.exit() self._init() cmd = [self.program] + self._parseArgs() + [’-d’,’-P’,self._pidFile()] p = Popen(cmd,stdout=PIPE) #self.pid = p.pid #self._writePid() print '%s start Sucessful tt [OK]' % self.name def _getPid(self): p = Popen([’pidof’,self.name],stdout=PIPE) pid = p.stdout.read().strip() return pid def stop(self): pid = self._getPid() if pid: os.kill(int(pid),15) if os.path.exists(self._pidFile()):os.remove(self._pidFile()) print '%s is stopped ttt [OK]' % self.name def restart(self): self.stop() self.start() def status(self): pid = self._getPid() if pid: print '%s is already running' % self.name else: print '%s is not running' % self.name def help(self): print 'Usage:%s {start|stop|status|restart|} ' % __file__ def main(): name = ’memcached’ prog = ’/usr/bin/memcached’ args = ’-u nobody -p 11211 -c 1024 -m 64’ wd = ’/var/tmp/memcached’ pm = Process(name = name, program = prog, workdir = wd) try: cmd = sys.argv[1] except IndexError,e: print 'Option error' sys.exit() if cmd == ’start’: pm.start() elif cmd == ’stop’: pm.stop() elif cmd == ’restart’: pm.restart() elif cmd == ’status’: pm.status() else: pm.help()if __name__ == ’__main__’: main()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Android table布局開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器2. IntelliJ IDEA安裝插件的方法步驟3. 理解PHP5中static和const關(guān)鍵字4. php模擬實(shí)現(xiàn)斗地主發(fā)牌5. spring acegi security 1.0.0 發(fā)布6. MyBatis中的JdbcType映射使用詳解7. vue 使用localstorage實(shí)現(xiàn)面包屑的操作8. Python random庫(kù)使用方法及異常處理方案9. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟10. Vuex localStorage的具體使用

網(wǎng)公網(wǎng)安備