之前在设计「Doodle 2」和开发「知乎日报」时,我面对最多的数据类型就是带 ID 的数据了。 在使用关系型数据库时,自增的主键可以满足这个需求,而在 Redis 中就稍微麻烦些了。 一直以来我都额外使用了一个计数器来存储 ID,例如(省略了很多代码,不影响阅
之前在设计「Doodle 2」和开发「知乎日报」时,我面对最多的数据类型就是带 ID 的数据了。在使用关系型数据库时,自增的主键可以满足这个需求,而在 Redis 中就稍微麻烦些了。
一直以来我都额外使用了一个计数器来存储 ID,例如(省略了很多代码,不影响阅读):
class IDModel(JSONModel): id = IntegerProperty() @classmethod def get_by_id(cls, entity_id): json_content = cls.redis_client.hget(cls._KEY, entity_id) if json_content: return cls.from_json(json_content) @classmethod def get_by_ids(cls, ids): if not ids: return [] results = cls.redis_client.hmget(cls._KEY, ids) return [cls.from_json(json_content) for json_content in results] @classmethod def get_next_id(cls): return MAX_ID.get_next_id(cls._KEY) def save(self): self._populate_default_attributes() self._save_self() def _populate_default_attributes(self): if self.id is None: self.id = self.get_next_id() def _save_self(self): self.redis_client.hset(self._KEY, self.id, self.to_json()) class MAX_ID(SimpleModel): @classmethod def get_next_id(cls, for_type, increment=1): return cls.redis_client.hincrby(cls._KEY, for_type, increment) @classmethod def get_max_id(cls, for_type): return int(cls.redis_client.hget(cls._KEY, for_type) or 0)这种方式的优点是简单,比较适合存储 ID 不连续的数据;缺点是使用了 hash,比较占内存。
刚才又想到一种方法,直接把数据存到 list 里,然后依靠其长度来定 ID,例如:
class IDModel(JSONModel): id = IntegerProperty() @classmethod def get_by_id(cls, entity_id): json_content = cls.redis_client.lindex(cls._KEY, entity_id) if json_content: return cls.from_json(json_content) @classmethod def get_by_ids(cls, ids): if not ids: return [] key = cls._KEY pipe = cls.redis_client.pipeline(transaction=False) for entity_id in ids: pipe.lindex(key, entity_id) results = pipe.execute() return [cls.from_json(json_content) for json_content in results] def save(self): key = self._KEY if self.id is None: with self.redis_client.pipeline() as pipe: try: pipe.watch(key) self.id = pipe.llen(key) + 1 pipe.multi() pipe.rpush(key, self.to_json()) pipe.execute() except Exception: self.id = None raise else: self.redis_client.lset(key, self.id, self.to_json())缺点大致有如下三点:
- ID 必须连续
- 依赖事务,导致 _populate_default_attributes() 的逻辑不好分离
- 获取多个 ID 对应的实体需要执行多条语句
原文地址:用 Redis 存储 ID 连续的数据, 感谢原作者分享。

todropaviewInmySQL,使用“ dropviewifexistsview_name;” andTomodifyAview,使用“ createOrreplaceViewViewViewview_nameAsSelect ...”。 whendroppingaview,asew dectivectenciesanduse和showcreateateviewViewview_name;“ tounderStanditSsstructure.whenModifying

mySqlViewScaneFectectialized unizedesignpatternslikeadapter,Decorator,Factory,andObserver.1)adapterPatternadaptSdataForomDifferentTablesIntoAunifiendView.2)decoratorPatternenhancateDataWithCalcalcualdCalcalculenfields.3)fieldfields.3)

查看InMysqlareBeneForsImplifyingComplexqueries,增強安全性,確保dataConsistency,andOptimizingPerformance.1)他們simimplifycomplexqueriesbleiesbyEncapsbyEnculatingThemintoreusableviews.2)viewsEnenenhancesecuritybyControllityByControllingDataAcces.3)

toCreateAsimpleViewInmySQL,USEthecReateaTeviewStatement.1)defitEtheetEtheTeViewWithCreatEaTeviewView_nameas.2)指定usethectstatementTorivedesireddata.3)usethectStatementTorivedesireddata.3)usetheviewlikeatlikeatlikeatlikeatlikeatlikeatable.views.viewssimplplifefifydataaccessandenenanceberity but consisterfort,butconserfort,consoncontorfinft

1)foralocaluser:createUser'localuser'@'@'localhost'Indidendify'securepassword'; 2)foraremoteuser:creationuser's creationuser'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Rocaluser'@'localhost'Indidendify'seceledify'Securepassword'; 2)

mysqlviewshavelimitations:1)他們不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinsOrsubqueries.2)他們canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

porthusermanagementinmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)複雜的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3漢化版
中文版,非常好用

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)