Skip to content

Commit

Permalink
Merge pull request #28 from art-by-city/23-legacy-memcache
Browse files Browse the repository at this point in the history
Adds legacy cache module
  • Loading branch information
jim-toth authored Sep 7, 2023
2 parents 7b37106 + b7e8357 commit 05b94bc
Show file tree
Hide file tree
Showing 8 changed files with 679 additions and 19 deletions.
159 changes: 159 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
"@types/chai": "^4.3.5",
"@types/chai-as-promised": "^7.1.5",
"@types/mocha": "^10.0.1",
"@types/sinon": "^10.0.16",
"@types/sinon-chai": "^3.2.9",
"@typescript-eslint/eslint-plugin": "^6.4.1",
"@typescript-eslint/parser": "^6.4.1",
"chai": "^4.3.8",
Expand All @@ -51,6 +53,8 @@
"opener": "^1.5.2",
"rimraf": "^5.0.1",
"selenium-webdriver": "^4.11.1",
"sinon": "^15.2.0",
"sinon-chai": "^3.7.0",
"ts-loader": "^9.4.4",
"ts-node": "^10.9.1",
"typescript": "^5.2.2",
Expand Down
5 changes: 5 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ export const ENVIRONMENTS = [
] as const
export type ArtByCityEnvironment = typeof ENVIRONMENTS[number]

export type CacheStrategy = 'disabled' | 'memcache'

export type ArtByCityConfig = {
environment: ArtByCityEnvironment
usernamesContractId: string
cache: {
type: CacheStrategy
}
}
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export default class ArtByCity {
: 'UHPC-7wenVg-JyS81EXKCnLlKvjSbfrIsnWt1F8hueg'

this.config = {
environment, usernamesContractId
environment,
usernamesContractId,
cache: {
type: config?.cache?.type === 'disabled' ? 'disabled' : 'memcache'
}
}

this.legacy = new ArtByCityLegacy(this.arweave, this.config)
Expand Down
67 changes: 55 additions & 12 deletions src/legacy/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ArtByCityConfig } from '../config'
import VerifiedCreators from './verified-creators.json'
import LegacyTransactions from './transactions'
import LegacyUsernames from './usernames'
import LegacyMemcache from './memcache'
import {
LegacyProfile,
LegacyBundlePublicationFeed,
Expand All @@ -18,16 +19,24 @@ import {
export default class ArtByCityLegacy {
private readonly transactions!: LegacyTransactions
public readonly usernames!: LegacyUsernames

constructor(
arweave: Arweave,
private readonly config: ArtByCityConfig
) {
private readonly cacheEnabled!: boolean
public readonly caches: {
publications: LegacyMemcache<LegacyPublicationManifest>
slugs: LegacyMemcache<string>
profiles: LegacyMemcache<LegacyProfile>
} = { /* eslint-disable indent */
publications: new LegacyMemcache<LegacyPublicationManifest>(),
slugs: new LegacyMemcache<string>(),
profiles: new LegacyMemcache<LegacyProfile>()
} /* eslint-enable indent */

constructor(arweave: Arweave, private readonly config: ArtByCityConfig) {
this.transactions = new LegacyTransactions(arweave, config.environment)
this.usernames = new LegacyUsernames(
config.usernamesContractId,
config.environment
)
this.cacheEnabled = config.cache.type === 'memcache'
}

get verifiedCreators(): string[] {
Expand Down Expand Up @@ -97,18 +106,25 @@ export default class ArtByCityLegacy {
}

async fetchPublicationBySlugOrId(
slugOrId: string
slugOrId: string,
useCache: boolean = true
): Promise<LegacyPublicationManifest> {
try {
return await this.fetchPublicationBySlug(slugOrId)
return await this.fetchPublicationBySlug(slugOrId, useCache)
} catch (error) { /* eslint-disable-line no-empty */ }

return this.fetchPublication(slugOrId)
return this.fetchPublication(slugOrId, useCache)
}

async fetchPublicationBySlug(
slug: string
slug: string,
useCache: boolean = true
): Promise<LegacyPublicationManifest> {
if (this.cacheEnabled && useCache) {
const cached = this.caches.slugs.get(slug)
if (cached) { return this.fetchPublication(cached) }
}

const { transactions } = await this
.transactions
.query('artwork', { tags: [ { name: 'slug', value: slug } ] })
Expand All @@ -117,12 +133,23 @@ export default class ArtByCityLegacy {
throw new Error(`404 Publication Not Found: slug://${slug}`)
}

return this.fetchPublication(transactions[0].id)
const id = transactions[0].id
if (this.cacheEnabled && useCache) {
this.caches.slugs.put(slug, id)
}

return this.fetchPublication(id, useCache)
}

async fetchPublication(
manifestId: string
manifestId: string,
useCache: boolean = true
): Promise<LegacyPublicationManifest> {
if (this.cacheEnabled && useCache) {
const cached = this.caches.publications.get(manifestId)
if (cached) { return cached }
}

const { data, ok } = await this.transactions.fetchData(manifestId)

if (!ok) {
Expand Down Expand Up @@ -271,10 +298,22 @@ export default class ArtByCityLegacy {
publication.model = data.model.model
}

if (this.cacheEnabled && useCache) {
this.caches.publications.put(manifestId, publication)
}

return publication
}

async fetchProfile(address: string): Promise<LegacyProfile | null> {
async fetchProfile(
address: string,
useCache: boolean = true
): Promise<LegacyProfile | null> {
if (this.cacheEnabled && useCache) {
const cached = this.caches.profiles.get(address)
if (cached) { return cached }
}

const {
transactions
} = await this.transactions.query('profile', { from: address, limit: 1 })
Expand Down Expand Up @@ -305,6 +344,10 @@ export default class ArtByCityLegacy {
profile.twitter = profile.x
}

if (this.cacheEnabled && useCache) {
this.caches.profiles.put(address, profile)
}

return profile
}

Expand Down
Loading

0 comments on commit 05b94bc

Please sign in to comment.