Skip to content

Commit

Permalink
fix: cache expire null not taking effect over default expire
Browse files Browse the repository at this point in the history
  • Loading branch information
NahumNoga authored Feb 2, 2023
1 parent 956f76e commit aedc132
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cli/src/languages/ts/__snapshots__/codegen-ts.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ exports[`ts codegen config can be null 1`] = `
import { MemorixClientApi } from \\"@memorix/client-redis\\";
// prettier-ignore
export class MemorixApi extends MemorixClientApi.fromConfig({defaultOptions:{cache:{}}}) {
export class MemorixApi extends MemorixClientApi.fromConfig({defaultOptions:{cache:{expire:undefined}}}) {
}"
`;
Expand Down
4 changes: 3 additions & 1 deletion cli/src/languages/ts/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { ValueType, ValueTypes } from "src/core/value";
import { assertUnreachable, getTabs } from "src/core/utilities";

export const jsonStringify: (json: any) => string = (json) =>
JSON.stringify(json).replace(/"([^"]+)":/g, "$1:");
JSON.stringify(json, (k, v) => (v === undefined ? null : v))
.replace(/null/g, "undefined")
.replace(/"([^"]+)":/g, "$1:");

const valueToTs: (
value: ValueType,
Expand Down
2 changes: 2 additions & 0 deletions clients/redis/js/src/example-schema.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export class MemorixApi extends MemorixClientApi.fromConfig({defaultOptions:{cac
user: this.getCacheItem<string, User>("user"),
// prettier-ignore
userExpire: this.getCacheItem<string, User>("userExpire", {expire:{value:1000,isInMs:true}}),
// prettier-ignore
userExpire2: this.getCacheItem<string, User>("userExpire2", {expire:undefined}),
};

pubsub = {
Expand Down
7 changes: 7 additions & 0 deletions clients/redis/js/src/example-schema.memorix
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ Cache {
}
}
}
userExpire2 {
key: string
payload: User
options: {
expire: null
}
}
}

PubSub {
Expand Down
6 changes: 6 additions & 0 deletions clients/redis/js/src/example-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ describe("example schema has", () => {
const user2 = await memorixApi.cache.userExpire.get("uv");
expect(user2).toBe(null);
});
it("cache expire none", async () => {
await memorixApi.cache.userExpire2.set("uv", { name: "uv", age: 29 });
await new Promise((res) => setTimeout(res, 2500));
const user = await memorixApi.cache.userExpire2.get("uv");
expect(user).not.toBe(null);
});
describe("pubsub", () => {
it("publish says how many subscribers", (done) => {
memorixApi.pubsub.message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ def merge(
item1: typing.Optional["CacheSetOptionsExpire"],
item2: typing.Optional["CacheSetOptionsExpire"],
) -> typing.Optional["CacheSetOptionsExpire"]:
if item2 is None:
return item1
return item2


Expand Down
7 changes: 7 additions & 0 deletions clients/redis/python/tests/example-schema.memorix
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ Cache {
}
}
}
userExpire2 {
key: string
payload: User
options: {
expire: null
}
}
}

PubSub {
Expand Down
56 changes: 56 additions & 0 deletions clients/redis/python/tests/example_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,62 @@ def test_cache_expire_defaults_config() -> None:
assert user2 is None


def test_cache_expire_none() -> None:
memorix_api = MemorixApi(
redis_url=redis_url,
)

memorix_api.cache.userExpire2.set(
"uv",
User(name="uv", age=29),
)

sleep(2.5)
user = memorix_api.cache.userExpire2.get("uv")
assert user is not None


def test_cache_expire_none_defaults() -> None:
memorix_api = MemorixApi(
redis_url=redis_url,
defaults=MemorixClientApiDefaults(
cache_set_options=MemorixClientCacheSetOptions(
expire=MemorixClientCacheSetOptionsExpire(value=1),
),
),
)

memorix_api.cache.userExpire2.set(
"uv",
User(name="uv", age=29),
)

sleep(1.5)
user = memorix_api.cache.userExpire2.get("uv")
assert user is not None


def test_cache_expire_none_code() -> None:
memorix_api = MemorixApi(
redis_url=redis_url,
defaults=MemorixClientApiDefaults(
cache_set_options=MemorixClientCacheSetOptions(
expire=MemorixClientCacheSetOptionsExpire(value=1),
),
),
)

memorix_api.cache.userExpire.set(
"uv",
User(name="uv", age=29),
options=MemorixClientCacheSetOptions(expire=None),
)

sleep(1.5)
user = memorix_api.cache.userExpire.get("uv")
assert user is not None


def test_pubsub() -> None:
memorix_api = MemorixApi(redis_url=redis_url)

Expand Down
8 changes: 8 additions & 0 deletions clients/redis/python/tests/example_schema_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ def __init__(self, api: MemorixClientApi) -> None:
),
),
)
self.userExpire2 = MemorixClientCacheApiItem[str, "User"](
api=self._api,
id="userExpire2",
payload_class=User,
options=MemorixClientCacheSetOptions(
expire=None,
),
)


class MemorixPubSubApi(MemorixClientPubSubApi):
Expand Down

0 comments on commit aedc132

Please sign in to comment.