Fungidzira isu tine system iyo vashandisi vanowedzera zvinyorwa. Kumushandisi wega wega, tinoratidza nhamba dzezvinyorwa zvavo mudhibhodhi ravo pachavo: nhamba yezvinyorwa, avhareji yekuverenga mazwi, mafambisirwo ekubudiswa, nezvimwewo. Kuti tikurumidze hurongwa, tinochengetedza iyi data. Yakasarudzika cache kiyi inogadzirwa kune yega yega report.
Mubvunzo unomuka: sei kumisa macache akadaro kana data rachinja? Imwe nzira ndeyekudzima cache yega yega chiitiko, semuenzaniso, kana chinyorwa chitsva chawedzerwa:
class InvalidateArticleReportCacheOnArticleCreated { public function handle(event: ArticleCreatedEvent): void { this->cacheService->deleteMultiple([ 'user_article_report_count_' . event->userId, 'user_article_report_word_avg_' . event->userId, 'user_article_report_freq_avg_' . event->userId, ]) } }
Iyi nzira inoshanda asi inova yakaoma pakubata nehuwandu hukuru hwemishumo nemakiyi. Apa ndipo panowanikwa tagged caching. Tagged caching inobvumira data kuti ibatanidzwe kwete nekiyi chete asiwo neakarongwa ema tag. Zvadaro, marekodhi ese ane hukama netag chaiyo anogona kusashanda, achirerutsa maitiro.
Kunyora kukosha kune cache nema tags:
this->taggedCacheService->set( key: 'user_article_report_count_' . user->id, value: value, tagNames: [ 'user_article_cache_tag_' . user->id, 'user_article_report_cache_tag_' . user->id, 'user_article_report' ] )
Kukanganisa cache nema tags:
class UpdateCacheTagsOnArticleCreated { public function handle(event: ArticleCreatedEvent): void { this->taggedCacheService->updateTagsVersions([ 'user_article_cache_tag_' . user->id, ]) } }
Pano, iyo tag 'user_article_cache_tag_' . $user->id
inomiririra shanduko muzvinyorwa zvemushandisi. Inogona kushandiswa kumisa chero cache zvinoenderana neiyi data. Imwe tag chaiyo 'user_article_report_cache_tag_' . $user->id
inobvumira chete mishumo yemushandisi kuti icheneswe, nepo general tag 'user_article_report'
isingaite mishumo cache kune vese vashandisi.
Kana yako caching library isingatsigire kumaka, unogona kuishandisa iwe pachako. Pfungwa huru ndeyekuchengetedza yazvino vhezheni kukosha kwema tags, pamwe neyega yega kukosha kwakaiswa, kuchengetedza iwo tag shanduro dzaivepo panguva iyo kukosha kwakanyorwa kune cache. Zvino, kana uchidzosa kukosha kubva kune cache, iyo yazvino tag vhezheni inodzoserwa zvakare, uye kutendeseka kwavo kunotariswa nekuzvienzanisa.
Kugadzira kirasi TaggedCache
class TaggedCache { private cacheService: cacheService }
Kuita iyo set
nzira yekunyora kune cache nema tag. Mune iyi nzira, isu tinofanirwa kunyora kukosha kune cache, pamwe nekudzoreredza mavhezheni azvino ematagi akapihwa uye woachengeta akabatana nechaiyo cache kiyi. Izvi zvinowanikwa nekushandisa kiyi yekuwedzera ine prefix inowedzerwa kune kiyi yakapihwa.
class TaggedCache { private cacheService: cacheService public function set( key: string, value: mixed, tagNames: string[], ttl: int ): bool { if (empty(tagNames)) { return false } tagVersions = this->getTagsVersions(tagNames) tagsCacheKey = this->getTagsCacheKey(key) return this->cacheService->setMultiple( [ key => value, tagsCacheKey => tagVersions, ], ttl ) } private function getTagsVersions(tagNames: string[]): array<string, string> { tagVersions = [] tagVersionKeys = [] foreach (tagNames as tagName) { tagVersionKeys[tagName] = this->getTagVersionKey(tagName) } if (empty(tagVersionKeys)) { return tagVersions } tagVersionsCache = this->cacheService->getMultiple(tagVersionKeys) foreach (tagVersionKeys as tagName => tagVersionKey) { if (empty(tagVersionsCache[tagVersionKey])) { tagVersionsCache[tagVersionKey] = this->updateTagVersion(tagName) } tagVersions[$tagName] = tagVersionsCache[tagVersionKey] } return tagVersions } private function getTagVersionKey(tagName: string): string { return 'tag_version_' . tagName } private function getTagsCacheKey(key: string): string { return 'cache_tags_tagskeys_' . key }
Kuwedzera iyo get
nzira yekudzoreredza akamaka kukosha kubva kune cache. Pano, tinotora kukosha tichishandisa kiyi, pamwe chete neshanduro dzemaki dzakabatanidzwa nekiyi iyoyo. Zvadaro tinotarisa kushanda kwema tags. Kana chero tag isiriyo, kukosha kunodzimwa kubva kucache uye null
inodzoserwa. Kana ma tag ese ariko, kukosha kwecached kunodzoserwa.
class TaggedCache { private cacheService: cacheService public function get(key: string): mixed { tagsCacheKey = this->getTagsCacheKey(key) values = this->cacheService->getMultiple([key, tagsCacheKey]) if (empty(values[key]) || empty(values[tagsCacheKey])) { return null } value = values[key] tagVersions = values[tagsCacheKey] if (! this->isTagVersionsValid(tagVersions)) { this->cacheService->deleteMultiple([key, tagsCacheKey]) return null } return value } private function isTagVersionsValid(tagVersions: array<string, string>): bool { tagNames = array_keys(tagVersions) actualTagVersions = this->getTagsVersions(tagNames) foreach (tagVersions as tagName => tagVersion) { if (empty(actualTagVersions[tagName])) { return false } if (actualTagVersions[tagName] !== tagVersion) { return false } } return true } }
Kuita iyo updateTagsVersions
nzira yekuvandudza tag shanduro. Pano, tinodzokorora pamusoro pemategi ese akapihwa uye nekuvandudza mavhezheni avo tichishandisa, semuenzaniso, nguva yazvino seshanduro.
class TaggedCache { private cacheService: cacheService public function updateTagsVersions(tagNames: string[]): void { foreach (tagNames as tagName) { this->updateTagVersion(tagName) } } private function updateTagVersion(tagName: string): string { tagKey = this->getTagVersionKey(tagName) tagVersion = this->generateTagVersion() return this->cacheService->set(tagKey, tagVersion) ? tagVersion : '' } private function generateTagVersion(): string { return (string) hrtime(true) } }
Iyi nzira iri nyore uye yepasirese. Tagged caching inobvisa kudiwa kwekutsanangura nemaoko makiyi ese ekukanganisa, otomatiki maitiro. Nekudaro, zvinoda zvimwe zviwanikwa: kuchengetedza tag vhezheni data uye kutarisa chokwadi chavo nechikumbiro chega chega.
Kana yako caching sevhisi ichikurumidza uye isina kumanikidzwa zvakanyanya muhukuru, nzira iyi haizokanganisa zvakanyanya kuita. Kuti uderedze mutoro, unogona kusanganisa tagged caching nemaitiro emunharaunda caching.
Nenzira iyi, tagged caching haingorerutsi kusashanda asiwo inoita kuti kushanda nedata kuve nyore uye kunzwisisika, kunyanya mumasisitimu akaomarara ane huwandu hukuru hwe data yakabatana.