Skip to content

Commit

Permalink
Merge pull request #31 from alexist/fix/30-incr-ttl-issues
Browse files Browse the repository at this point in the history
fix(#30): INCR command TTL issue due to lastingTime/deadline calculation
  • Loading branch information
grrolland authored Nov 22, 2023
2 parents 5dd862b + 86db1c1 commit 2c5454d
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 206 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<vertx.version>4.4.5</vertx.version>
<hazelcast.version>5.3.1</hazelcast.version>
<slf4j.version>2.0.4</slf4j.version>
<logback.version>1.4.11</logback.version>
<logback.version>1.3.11</logback.version>
<junit.version>4.13.2</junit.version>
<resilience4j-retry.version>1.7.0</resilience4j-retry.version>
</properties>
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/io/github/grrolland/hcshm/ShmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ public String get(String key) {
* @param value
* the value as string
* @param expire
* the expiration in seconds
* the expiration in milliseconds
* @return the value set
*/
public String set(String key, String value, int expire) {
getMap(key).set(key, new ShmValue(value, expire), expire, TimeUnit.SECONDS);
public String set(String key, String value, long expire) {
getMap(key).set(key, new ShmValue(value, expire), expire, TimeUnit.MILLISECONDS);
return value;
}

Expand All @@ -89,12 +89,12 @@ public String set(String key, String value, int expire) {
* @param value
* the value as long
* @param expire
* the expiration in seconds
* the expiration in milliseconds
* @return the value set as string representation
*/
public String set(String key, long value, int expire) {
public String set(String key, long value, long expire) {
String r = Long.toString(value);
getMap(key).set(key, new ShmValue(r, expire), expire, TimeUnit.SECONDS);
getMap(key).set(key, new ShmValue(r, expire), expire, TimeUnit.MILLISECONDS);
return r;
}

Expand All @@ -104,9 +104,9 @@ public String set(String key, long value, int expire) {
* @param key
* the key
* @param expire
* the expiration in seconds
* the expiration in milliseconds
*/
public void touch(String key, int expire) {
public void touch(String key, long expire) {
getMap(key).executeOnKey(key, new TouchProcessor(expire));
}

Expand All @@ -123,7 +123,7 @@ public void touch(String key, int expire) {
* the initial expiration
* @return the new value as string representation
*/
public String incr(String key, int value, int init, int initialExpire) {
public String incr(String key, int value, int init, long initialExpire) {
return (String) getMap(key).executeOnKey(key, new IncrProcessor(value, init, initialExpire));
}

Expand Down
89 changes: 43 additions & 46 deletions src/main/java/io/github/grrolland/hcshm/ShmValue.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
/**
* ngx-distributed-shm
* Copyright (C) 2018 Flu.Tech
*
* <p>
* This program 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.
*
* <p>
* This program 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.
*
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.grrolland.hcshm;


import java.io.Serializable;

/**
Expand All @@ -28,77 +27,75 @@ public class ShmValue implements Serializable {
/**
* The value
*/
private String value = null;
private final String value;

/**
* The expiration deadline
*/
private long deadline = 0;

/**
* Indicate if the value expire
*/
private boolean doExpire = false;

/**
* Constructor
*
* @param newval the new value
* @param expire the expiration in second
*/
public ShmValue(String newval, int expire) {
value = newval;
if (expire != 0) {
deadline = System.currentTimeMillis() + expire * 1000;
doExpire = true;
}
}

/**
* Get the value
*
* @return the value
*/
public String getValue() {
return value;
}

/**
* Expire the value
* @param sectime expiration in second
*/
public void expire(int sectime) {
if (sectime != 0) {
deadline = System.currentTimeMillis() + sectime * 1000;
doExpire = true;
}
else
{
doExpire = false;
}
}

/**
* Get le lasting time for the value
*
* When the time to the expiration deadline is lower than 1, return -1.
*
* <p>
* When the time to the expiration deadline is lower than 1000, return -1.
* <p>
* tha mean the value should expire immediately
*
* @return the time to the expiration deadline
*/
public int getLastingTime() {
public long getLastingTime() {
if (doExpire) {
final double lt = (deadline - System.currentTimeMillis()) / 1000d;
if (lt < 1.0) {
final long lt = deadline - System.currentTimeMillis();
if (lt < 1000) {
return -1;
} else {
return lt;
}
else {
return (int) lt;
}
}
else
{
} else {
return 0;
}
}

/**
* Constructor
*
* @param newval
* the new value
* @param expire
* the expiration in milliseconds
*/
public ShmValue(String newval, long expire) {
value = newval;
this.expire(expire);
}

/**
* Expire the value
*
* @param sectime
* expiration in milliseconds
*/
public void expire(long sectime) {
if (sectime != 0) {
deadline = System.currentTimeMillis() + sectime;
doExpire = true;
} else {
doExpire = false;
}
}

}
Loading

0 comments on commit 2c5454d

Please sign in to comment.