Skip to content

Commit

Permalink
chore: fix ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
vladanpaunovic committed Sep 5, 2024
1 parent 23cab33 commit 7f30b5f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 29 deletions.
2 changes: 1 addition & 1 deletion components/InputForm/InputForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ const InputForm = () => {
</label>
{isDateBeforeFirstMarketData && (
<p className="text-yellow-700 text-xs mt-2">
Sorry, we only have {currentCoin.name} market data starting from{" "}
Sorry, we only have {currentCoin?.name} market data starting from{" "}
{dateOfFirstMarketData.format("MM-DD-YYYY")}. We&apos;ve adjusted
the date you entered to match.
</p>
Expand Down
4 changes: 2 additions & 2 deletions components/SelectCoin/SelectCoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ export const getSelectTheme = (theme) => {
const parseOptions = (options) => {
return options.map((option) => ({
...option,
value: option.coinId,
value: option?.coinId,
label: (
<span className="flex items-center">
<span className="text-xs text-gray-300 mr-2">
#{option.marketCapRank}
#{option?.marketCapRank}
</span>{" "}
<span className="text-gray-900 ">{option.name}</span>
</span>
Expand Down
12 changes: 6 additions & 6 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export const defaultCurrency = availableCurrencies[0].value;

export const CACHE_INVALIDATION_INTERVAL = 3600; // 1 hour

export const WEBSITE_URL =
process.env.NEXT_PUBLIC_VERCEL_ENV === "production"
? "www.dca-cc.com"
: process.env.NEXT_PUBLIC_VERCEL_URL;
export const IS_PROD = process.env.NEXT_PUBLIC_VERCEL_ENV === "production";

export const WEBSITE_PREFIX =
process.env.NEXT_PUBLIC_VERCEL_ENV === "production" ? "https://" : "http://";
export const WEBSITE_URL = IS_PROD
? "www.dca-cc.com"
: process.env.NEXT_PUBLIC_VERCEL_URL;

export const WEBSITE_PREFIX = IS_PROD ? "https://" : "http://";
export const WEBSITE_PATHNAME = WEBSITE_PREFIX + WEBSITE_URL;

export const WEBSITE_EMAIL = "dcacryptocurrency@gmail.com";
Expand Down
54 changes: 34 additions & 20 deletions server/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,35 @@ const rawRedis = new Redis({

export const upstashAdopter = UpstashRedisAdapter(rawRedis);

const generateFingerprintString = (fingerprint) => `fingerprint:${fingerprint}`;
const generateFingerprintKey = (fingerprint) => `fingerprint:${fingerprint}`;

export const storeFingerprint = async (fingerprint) => {
const redisKey = generateFingerprintString(fingerprint);
const updateRedisKey = async (key, value, ttl) => {
await rawRedis.set(key, value, { ex: ttl });
};

const currentRedisKey = await rawRedis.get(redisKey);
const getOrInitializeRedisKey = async (key, defaultValue, ttl) => {
const redisValue = await rawRedis.get(key);
if (redisValue) return redisValue;

await updateRedisKey(key, defaultValue, ttl);
return defaultValue;
};

if (currentRedisKey) {
await rawRedis.set(redisKey, currentRedisKey + 1, {
ex: FREE_TIER_REDIS_TTL,
});
} else {
await rawRedis.set(redisKey, 1, { ex: FREE_TIER_REDIS_TTL });
export const storeFingerprint = async (fingerprint) => {
const redisKey = generateFingerprintKey(fingerprint);
const currentCount = await getOrInitializeRedisKey(
redisKey,
1,
FREE_TIER_REDIS_TTL
);

// Only update the count if it's below the limit
if (parseInt(currentCount) <= FREE_TIER_CALCULATION_LIMIT) {
await updateRedisKey(
redisKey,
parseInt(currentCount) + 1,
FREE_TIER_REDIS_TTL
);
}
};

Expand All @@ -32,23 +48,21 @@ export const canUserProceed = async (fingerprint, session) => {
return { proceed: true, package: session.user.subscription.subId };
}

const redisKey = generateFingerprintString(fingerprint);

const redisResponse = await rawRedis.get(redisKey);
const sessionUserCount = redisResponse || 1;
const redisKey = generateFingerprintKey(fingerprint);
const sessionUserCount = await getOrInitializeRedisKey(
redisKey,
1,
FREE_TIER_REDIS_TTL
);

if (sessionUserCount > FREE_TIER_CALCULATION_LIMIT) {
if (parseInt(sessionUserCount) > FREE_TIER_CALCULATION_LIMIT) {
const ttl = await rawRedis.ttl(redisKey);
return { proceed: false, ttl, error: "limit reached" };
}

if (!redisResponse) {
rawRedis.set(redisKey, 1, { ex: FREE_TIER_REDIS_TTL });
}

return {
proceed: true,
sessionUserCount,
sessionUserCount: parseInt(sessionUserCount),
available: FREE_TIER_CALCULATION_LIMIT,
};
};
Expand Down

0 comments on commit 7f30b5f

Please sign in to comment.