Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

"""LICENSE 

Copyright 2018 Hermann Krumrey <hermann@krumreyh.com> 

 

This file is part of anime-list-apis. 

 

anime-list-apis is free software: you can redistribute it and/or modify 

it under the terms of the GNU General Public License as published by 

the Free Software Foundation, either version 3 of the License, or 

(at your option) any later version. 

 

anime-list-apis is distributed in the hope that it will be useful, 

but WITHOUT ANY WARRANTY; without even the implied warranty of 

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

GNU General Public License for more details. 

 

You should have received a copy of the GNU General Public License 

along with anime-list-apis. If not, see <http://www.gnu.org/licenses/>. 

LICENSE""" 

 

import os 

import time 

import json 

from copy import deepcopy 

from typing import Dict, Any, Optional 

from anime_list_apis.models.Serializable import Serializable 

from anime_list_apis.models.attributes.Id import IdType, Id 

from anime_list_apis.models.attributes.MediaType import MediaType 

from anime_list_apis.models.MediaData import MediaData 

from anime_list_apis.models.MediaUserData import MediaUserData 

from anime_list_apis.models.MediaListEntry import MediaListEntry 

from anime_list_apis.models.CacheAble import CacheModelType, CacheAble 

 

 

class Cache: 

""" 

Handles various caching functionality 

""" 

 

model_map = { 

CacheModelType.DATA: None, 

CacheModelType.MEDIA_DATA: MediaData, 

CacheModelType.MEDIA_USER_DATA: MediaUserData, 

CacheModelType.MEDIA_LIST_ENTRY: MediaListEntry 

} 

""" 

Maps model types to their respective classes 

""" 

 

def __init__( 

self, 

cache_location: str = None, 

expiration: int = 6000, 

write_after: int = 20 

): 

""" 

Initializes the Cache. If the Cache directory and file do not exist, 

they will be created here. 

:param cache_location: The location of the cache. Will default to a 

hidden directory in the user's home directory 

:param expiration: Defines how long objects should be valid. 

If set to a negative number, will be infinite 

:param write_after: Defines after how many cache changes the changes 

are automatically written to the cache file 

""" 

self.expiration = expiration 

self.write_after = write_after 

self.change_count = 0 

 

if cache_location is None: # pragma: no cover 

self.cache_location = os.path.join( 

os.path.expanduser("~"), ".anime_list_apis" 

) 

else: 

self.cache_location = cache_location 

self.cache_file = os.path.join(self.cache_location, "cache.json") 

 

self.__cache = self.__generate_empty_cache() 

 

if not os.path.isdir(self.cache_location): 

os.makedirs(self.cache_location) 

 

if not os.path.isfile(self.cache_file): 

self.write() 

 

self.load() 

 

def write(self): 

""" 

Writes the content of the cache to the cache file 

:return: None 

""" 

serialized = {} 

 

for model_type in self.__cache: 

serialized[model_type.name] = {} 

 

for site_type in self.__cache[model_type]: 

serialized[model_type.name][site_type.name] = {} 

 

if model_type == CacheModelType.DATA: 

serialized[model_type.name][site_type.name] = \ 

self.__cache[model_type][site_type] 

 

else: 

 

for tag in self.__cache[model_type][site_type]: 

serialized[model_type.name][site_type.name][tag] = { 

"timestamp": 

self.__cache[model_type][site_type][tag] 

["timestamp"], 

"data": 

self.__cache[model_type][site_type][tag] 

["data"].serialize() 

} 

 

with open(self.cache_file, "w") as f: 

json.dump( 

serialized, 

f, 

sort_keys=True, 

indent=4, 

separators=(",", ": ") 

) 

 

def load(self): 

""" 

Loads the content of the cache file into memory 

:return: None 

""" 

with open(self.cache_file, "r") as f: 

serialized = json.load(f) 

 

self.__cache = self.__generate_empty_cache() 

 

for _model_type, cache_data in serialized.items(): 

model_type = CacheModelType[_model_type] 

data_class = self.model_map[model_type] # type: Serializable 

 

for _site_type, site_data in cache_data.items(): 

site_type = IdType[_site_type] 

 

if model_type == CacheModelType.DATA: 

self.__cache[model_type][site_type] = site_data 

 

else: 

for tag, entry in site_data.items(): 

self.__cache[model_type][site_type][tag] = { 

"timestamp": entry["timestamp"], 

"data": data_class.deserialize(entry["data"]) 

} 

 

def add_primitive(self, site_type: IdType, key: str, value: Any): 

""" 

Adds a primitive data object to the cache 

:param site_type: The site for which to cache 

:param key: The key of the value 

:param value: the value to cache 

:return: None 

""" 

self.__cache[CacheModelType.DATA][site_type][key] = { 

"timestamp": time.time(), 

"value": value 

} 

 

def add( 

self, 

site_type: IdType, 

data: CacheAble, 

ignore_for_write_count: bool = False 

): 

""" 

Adds a copy of an object to the cache. 

If the amount of changes exceeds the amount defined in write_after, 

write to file afterwards 

:param site_type: The site for which to cache it 

:param data: The data to cache 

:param ignore_for_write_count: If set to True, will not increment the 

change_count variable. 

:return: None 

""" 

if data.get_model_type() == CacheModelType.MEDIA_LIST_ENTRY: 

data = data # type: MediaListEntry 

self.add(site_type, data.get_media_data(), ignore_for_write_count) 

self.add(site_type, data.get_user_data(), ignore_for_write_count) 

 

else: 

if data.get_model_type() == CacheModelType.MEDIA_USER_DATA: 

username = data.get_username() 

else: 

username = None 

 

_id = data.get_id().get(site_type) 

tag = self.generate_id_tag(data.get_media_type(), _id, username) 

 

self.__cache[data.get_model_type()][site_type][tag] = { 

"timestamp": time.time(), 

"data": deepcopy(data) 

} 

if not ignore_for_write_count: 

self.change_count += 1 

 

if self.change_count >= self.write_after: 

self.write() 

 

def get_primitive(self, site_type: IdType, key: str) -> Optional[Any]: 

""" 

Retrieves a primitive data object from the cache 

:param site_type: The site for which to get the cached data 

:param key: The key to retrieve 

:return: The cached primitive data object or None if no entry in cache 

""" 

if key in self.__cache[CacheModelType.DATA][site_type]: 

 

entry = self.__cache[CacheModelType.DATA][site_type][key] 

 

if time.time() - entry["timestamp"] > self.expiration >= 0: 

self.__cache[CacheModelType.DATA][site_type].pop(key) 

return None 

else: 

return entry["value"] 

else: 

return None 

 

def get( 

self, 

model_type: CacheModelType, 

site_type: IdType, 

media_type: MediaType, 

_id: int or Id, 

username: Optional[str] = None 

) -> Optional[CacheAble]: 

""" 

Retrieves a cached object. 

If the object has expired, remove it from the cache 

:param model_type: The cached data type 

:param site_type: The site type for which the object was cached 

:param media_type: The media type of the object to get 

:param _id: The ID to search for 

:param username: Optional-The username associated with the data object 

:return: A copy of the cached object, or None if it wasn't found 

""" 

if model_type == CacheModelType.MEDIA_LIST_ENTRY: 

media = self.get( 

CacheModelType.MEDIA_DATA, 

site_type, 

media_type, 

_id 

) 

user = self.get( 

CacheModelType.MEDIA_USER_DATA, 

site_type, 

media_type, 

_id, 

username 

) 

try: 

media_cls = MediaListEntry.get_class_for_media_type(media_type) 

return media_cls(media, user) 

except (ValueError, TypeError): 

return None 

 

else: 

_id = self.__resolve_id(site_type, _id) 

tag = self.generate_id_tag(media_type, _id, username) 

 

if tag in self.__cache[model_type][site_type]: 

entry = self.__cache[model_type][site_type][tag] 

timestamp = entry["timestamp"] 

 

if time.time() - timestamp > self.expiration >= 0: 

self.__cache[model_type][site_type].pop(tag) 

return None 

else: 

return deepcopy(entry["data"]) 

else: 

return None 

 

def get_media_data( 

self, 

site_type: IdType, 

media_type: MediaType, 

_id: int or Id 

) -> Optional[MediaData]: 

""" 

Retrieves a media data entry from the cache 

:param site_type: The site for which to fetch an entry 

:param media_type: The type of the media 

:param _id: The ID to fetch 

:return: The entry data or None if no corresponding entry exists 

""" 

return self.get( 

CacheModelType.MEDIA_DATA, 

site_type, 

media_type, 

_id 

) 

 

def get_media_user_data( 

self, 

site_type: IdType, 

media_type: MediaType, 

_id: int or Id, 

username: str 

) -> Optional[MediaUserData]: 

""" 

Retrieves a media user entry from the cache 

:param site_type: The site type for which the object was cached 

:param media_type: The media type of the cached object 

:param _id: The ID of the corresponding media data 

:param username: The username of the entry 

:return: The entry or None if not found 

""" 

return self.get( 

CacheModelType.MEDIA_USER_DATA, 

site_type, 

media_type, 

_id, 

username 

) 

 

def get_media_list_entry( 

self, 

site_type: IdType, 

media_type: MediaType, 

_id: int or Id, 

username: str 

) -> Optional[MediaListEntry]: 

""" 

Retrieves a media list entry from the cache 

:param site_type: The site for which to fetch the cached object 

:param media_type: The media type of the entry 

:param _id: The ID of the entry 

:param username: The username of the entry 

:return: The entry or None if not found 

""" 

return self.get( 

CacheModelType.MEDIA_LIST_ENTRY, 

site_type, 

media_type, 

_id, 

username 

) 

 

def invalidate( 

self, 

model_type: CacheModelType, 

site_type: IdType, 

media_type: MediaType, 

_id: int or Id, 

username: Optional[str] = None 

): 

""" 

Invalidates a cache entry 

:param model_type: The model type of the entry to invalidate 

:param site_type: The site type of the entry to invalidate 

:param media_type: The media type of the entry 

:param _id: The ID of the entry 

:param username: The username for which to invalidate the entry 

:return: None 

""" 

if model_type == CacheModelType.MEDIA_LIST_ENTRY: 

self.invalidate( 

CacheModelType.MEDIA_DATA, 

site_type, 

media_type, 

_id 

) 

self.invalidate( 

CacheModelType.MEDIA_USER_DATA, 

site_type, 

media_type, 

_id, 

username 

) 

else: 

_id = self.__resolve_id(site_type, _id) 

tag = self.generate_id_tag(media_type, _id, username) 

 

if tag in self.__cache[model_type][site_type]: 

self.__cache[model_type][site_type].pop(tag) 

 

# Write to make sure that cache entry is no longer accessible 

self.write() 

 

def invalidate_media_data( 

self, 

site_type: IdType, 

media_type: MediaType, 

_id: int or Id 

): 

""" 

Invalidates a media entry 

:param site_type: The site type of the entry 

:param media_type: The media type of the entry 

:param _id: The ID of the entry 

:return: None 

""" 

self.invalidate(CacheModelType.MEDIA_DATA, site_type, media_type, _id) 

 

def invalidate_media_user_data( 

self, 

site_type: IdType, 

media_type: MediaType, 

_id: int or Id, 

username: str 

): 

""" 

Invalidates a user data entry 

:param site_type: The site type of the entry 

:param media_type: The media type of the entry 

:param _id: The ID of the entry 

:param username: The user for which to invalidate the entry 

:return: None 

""" 

self.invalidate( 

CacheModelType.MEDIA_USER_DATA, 

site_type, 

media_type, 

_id, 

username 

) 

 

def invalidate_media_list_entry( 

self, 

site_type: IdType, 

media_type: MediaType, 

_id: int or Id, 

username: str 

): 

""" 

Invalidates a list entry 

:param site_type: The site type of the entry 

:param media_type: The media type of the entry 

:param _id: The ID of the entry 

:param username: The user for which to invalidate the entry 

:return: None 

""" 

self.invalidate( 

CacheModelType.MEDIA_LIST_ENTRY, 

site_type, 

media_type, 

_id, 

username 

) 

 

@staticmethod 

def __generate_empty_cache() \ 

-> Dict[ 

CacheModelType, Dict[ 

IdType, Dict[ 

str, Dict[ 

str, int or Serializable 

] 

] 

] 

]: 

""" 

Generates a fresh cache. 

The cache has the following structure: 

 

{ 

CacheModelType: { 

SiteType: { 

MediaType + Id: { 

"timestamp": float, 

"data": data 

} 

} 

} 

} 

 

The CacheModelType 'MediaListEntry' will be split into their 

'MediaData' and 'MediaUserData' components, so there will be no 

separate entry for them. 

Additionally, a 'data' entry will be added for caching various 

primitive data 

:return: The generated cache dictionary 

""" 

cache = {CacheModelType.DATA: {}} 

for site_type in IdType: 

cache[CacheModelType.DATA][site_type] = {} 

 

for model_type in CacheModelType: 

 

if model_type == CacheModelType.MEDIA_LIST_ENTRY: 

continue 

cache[model_type] = {} 

 

for site_type in IdType: 

cache[model_type][site_type] = {} 

return cache 

 

@staticmethod 

def __resolve_id(site_type: IdType, _id: int or Id) -> int: 

""" 

Turns an ID into an int if it is not already 

:param site_type: The type of site for which to store the ID 

:param _id: The ID to convert 

:return: The int ID 

""" 

if not isinstance(_id, int): 

return _id.get(site_type) 

else: 

return _id 

 

@staticmethod 

def generate_id_tag( 

media_type: MediaType, 

_id: int, 

username: Optional[str] = None 

) -> str: 

""" 

Generates an ID tag for the cache 

:param media_type: The media type of the tag 

:param _id: The ID of the entry 

:param username: Optionally create a tag for a specific username 

:return: The generated ID tag 

""" 

tag = media_type.name + "-" + str(_id) 

if username is not None: 

tag += "-" + username 

return tag