Skip to content

Commit 7f5aa21

Browse files
dyhdyh
authored andcommitted
添加lru测试实例
1 parent 47d2f44 commit 7f5aa21

File tree

11 files changed

+423
-0
lines changed

11 files changed

+423
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cache;
2+
3+
/**
4+
* @author dengyh
5+
* @version 1.0
6+
* @date 2024/12/8 12:09
7+
* @description
8+
*/
9+
public class LruCacheTest1 {
10+
public static void main(String[] args) {
11+
LruCacheWithExpiration cache = new LruCacheWithExpiration(5, 3000);
12+
cache.put("key1", "value1");
13+
cache.put("key2", "value2");
14+
cache.put("key3", "value3");
15+
System.out.println("获取key2的值: " + cache.get("key2"));
16+
17+
// 模拟等待4秒,让部分数据过期
18+
try {
19+
Thread.sleep(4000);
20+
} catch (InterruptedException e) {
21+
e.printStackTrace();
22+
}
23+
24+
// 验证过期后获取不存在的键
25+
System.out.println("获取key1的值: " + cache.get("key1"));
26+
27+
// 再次放入新键值对,触发LRU淘汰和过期清理
28+
cache.put("key6", "value6");
29+
30+
// 验证被淘汰的键不存在
31+
System.out.println("获取key3的值: " + cache.get("key3"));
32+
33+
// 验证新放入的键能获取到
34+
System.out.println("获取key6的值: " + cache.get("key6"));
35+
36+
}
37+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cache;
2+
3+
/**
4+
* @author dengyh
5+
* @version 1.0
6+
* @date 2024/12/8 12:15
7+
* @description
8+
*/
9+
public class LruCacheTest2 {
10+
public static void main(String[] args) {
11+
LruCacheWithExpiration cache = new LruCacheWithExpiration(4, 5000);
12+
13+
cache.put("key1", "value1");
14+
cache.put("key2", "value2");
15+
cache.put("key3", "value3");
16+
cache.put("key4", "value4");
17+
18+
// 多次获取key2,使其成为最近使用的元素
19+
for (int i = 0; i < 3; i++) {
20+
System.out.println("第" + (i + 1) + "次获取key2的值: " + cache.get("key2"));
21+
}
22+
23+
cache.put("key5", "value5");
24+
25+
// 验证key2因频繁被获取而未被淘汰,仍能获取到
26+
System.out.println("获取key2的值: " + cache.get("key2"));
27+
28+
// 验证最早放入且少被使用的元素(比如key1)被淘汰了
29+
System.out.println("获取key1的值: " + cache.get("key1"));
30+
31+
}
32+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cache;
2+
3+
/**
4+
* @author dengyh
5+
* @version 1.0
6+
* @date 2024/12/8 12:15
7+
* @description
8+
*/
9+
public class LruCacheTest3 {
10+
public static void main(String[] args) {
11+
LruCacheWithExpiration cache = new LruCacheWithExpiration(3, 2000);
12+
13+
cache.put("key1", "value1");
14+
cache.put("key2", "value2");
15+
cache.put("key3", "value3");
16+
17+
// 更新key2的值,应将其移到最近使用的位置
18+
cache.put("key2", "new_value2");
19+
20+
try {
21+
Thread.sleep(1500);
22+
} catch (InterruptedException e) {
23+
e.printStackTrace();
24+
}
25+
26+
cache.put("key4", "value4");
27+
28+
// 验证更新后,key2未因LRU被淘汰,且旧值被替换
29+
System.out.println("获取key2的值: " + cache.get("key2"));
30+
31+
try {
32+
Thread.sleep(1000);
33+
} catch (InterruptedException e) {
34+
e.printStackTrace();
35+
}
36+
37+
// 验证部分元素过期后,缓存的情况
38+
System.out.println("获取key1的值: " + cache.get("key1"));
39+
System.out.println("获取key3的值: " + cache.get("key3"));
40+
41+
42+
}
43+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package cache;
2+
3+
import java.util.HashMap;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* @author dengyh
10+
* @version 1.0
11+
* @date 2024/12/8 11:51
12+
* @description
13+
*/
14+
public class LruCacheWithExpiration {
15+
private int capacity;
16+
17+
private final Map<Object, LruNode> cache;
18+
19+
private final LinkedList<LruNode> linkedList;
20+
21+
private final long expireTime;
22+
23+
public LruCacheWithExpiration(int capacity, long expireTime) {
24+
this.capacity = capacity;
25+
this.cache = new HashMap<>();
26+
this.linkedList = new LinkedList<>();
27+
this.expireTime = expireTime;
28+
}
29+
30+
//put
31+
public void put(Object key, Object val){
32+
LruNode node;
33+
if(cache.containsKey(key)){
34+
node = cache.get(key);
35+
node.value = val;
36+
node.timestamp = System.currentTimeMillis();
37+
linkedList.remove(node);
38+
linkedList.addFirst(node);
39+
}else {
40+
node = new LruNode(key, val);
41+
if(cache.size() >= capacity){
42+
removeExipredAndLastUsed();
43+
}
44+
cache.put(key, node);
45+
linkedList.addFirst(node);
46+
}
47+
}
48+
49+
//删除过期的最近的
50+
public void removeExipredAndLastUsed() {
51+
while (!linkedList.isEmpty()){
52+
LruNode node = linkedList.getLast();
53+
if(isExpired(node)){
54+
linkedList.removeLast();
55+
cache.remove(node.key);
56+
}else {
57+
break;
58+
}
59+
}
60+
if(!linkedList.isEmpty()){
61+
LruNode nodeToRemove = linkedList.getLast();
62+
linkedList.removeLast();
63+
cache.remove(nodeToRemove.key);
64+
}
65+
}
66+
67+
//是否过期
68+
public boolean isExpired(LruNode node) {
69+
return System.currentTimeMillis() - node.timestamp > expireTime;
70+
}
71+
72+
//get
73+
public Object get(Object key){
74+
if(cache.containsKey(key)){
75+
LruNode node = cache.get(key);
76+
node.timestamp = System.currentTimeMillis();
77+
linkedList.remove(node);
78+
linkedList.addFirst(node);
79+
return node.value;
80+
}
81+
return null;
82+
}
83+
84+
public static void main(String[] args) {
85+
86+
}
87+
}

java/java_test/src/cache/LruNode.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cache;
2+
3+
/**
4+
* @author dengyh
5+
* @version 1.0
6+
* @date 2024/12/8 11:50
7+
* @description
8+
*/
9+
public class LruNode {
10+
public Object key;
11+
public Object value;
12+
public long timestamp;
13+
public LruNode(Object key, Object value) {
14+
this.key = key;
15+
this.value = value;
16+
this.timestamp = System.currentTimeMillis();
17+
}
18+
}

java/java_test/src/guc/Counter.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package guc;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.atomic.AtomicInteger;
6+
7+
/**
8+
* @author dengyh
9+
* @version 1.0
10+
* @date 2024/12/6 22:38
11+
* @description 原子操作
12+
*/
13+
public class Counter {
14+
private int i = 0;
15+
private AtomicInteger atomicInteger = new AtomicInteger(0);
16+
public static void main(String[] args) {
17+
final Counter cas = new Counter();
18+
List<Thread> ts = new ArrayList<>(600);
19+
long start = System.currentTimeMillis();
20+
for (int i = 0; i < 100; i++){
21+
Thread t = new Thread(new Runnable() {
22+
23+
@Override
24+
public void run() {
25+
for (int i = 0; i < 1000; i ++){
26+
cas.count();
27+
cas.safeCount();
28+
}
29+
}
30+
});
31+
ts.add(t);
32+
}
33+
for (Thread t :ts){
34+
t.start();
35+
}
36+
//所有线程完成
37+
for (Thread t: ts){
38+
try{
39+
t.join();
40+
}catch (InterruptedException e){
41+
e.printStackTrace();
42+
}
43+
}
44+
System.out.println(String.format("计数器:%s", cas.i));
45+
System.out.println(String.format("value:%s", cas.atomicInteger.get()));
46+
System.out.println(String.format("耗时:%s", System.currentTimeMillis() - start));
47+
}
48+
49+
/**
50+
* 非线程安全计数器
51+
* */
52+
public void count(){
53+
i++;
54+
}
55+
56+
/**
57+
* CAS线程安全计数器
58+
* */
59+
public void safeCount(){
60+
for (;;){
61+
int i = atomicInteger.get();
62+
boolean suc = atomicInteger.compareAndSet(i , ++i);
63+
if (suc){
64+
break;
65+
}
66+
}
67+
}
68+
}

java/java_test/src/test/Solution.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package test;
2+
3+
import java.lang.reflect.Array;
4+
import java.util.Arrays;
5+
6+
/**
7+
* @author dengyh
8+
* @version 1.0
9+
* @date 2024/12/5 16:45
10+
* @description
11+
*/
12+
public class Solution {
13+
public static String getPermutation(int n, int k) {
14+
int fact[] = new int[n];
15+
fact[0] = 1;
16+
for (int i = 1; i < n; ++i){
17+
System.out.println("i:" + i);
18+
fact[i] = fact[i-1]*i;
19+
}
20+
// System.out.println(Arrays.toString(fact));
21+
k = k - 1;
22+
StringBuffer ans = new StringBuffer();
23+
int[] valid = new int[n + 1];
24+
Arrays.fill(valid, 1);
25+
System.out.println(Arrays.toString(fact));
26+
System.out.println(Arrays.toString(valid));
27+
28+
for (int i = 1; i <= n; ++i){
29+
int order = k / fact[n - i] + 1;
30+
System.out.println(order);
31+
for (int j = 1; j <= n; ++ j){
32+
order -= valid[j];
33+
if (order == 0){
34+
ans.append(j);
35+
valid[j] = 0;
36+
break;
37+
}
38+
}
39+
k %= fact[n - i];
40+
System.out.println("k:" + k);
41+
}
42+
return ans.toString();
43+
}
44+
45+
public static void main(String[] args) {
46+
int n = 3;
47+
int k = 3;
48+
String res = getPermutation(n, k);
49+
System.out.println(res);
50+
}
51+
}

java/java_test/src/test/Test.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package test;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @author dengyh
7+
* @version 1.0
8+
* @date 2024/12/5 14:26
9+
* @description
10+
*
11+
* 连续子数组最大和
12+
*
13+
* 输入:[-1, 1, 2, 3, -4, 0, 5, -2]
14+
*
15+
* 输出: 7 ([1, 2, 3, -4, 0, 5] )
16+
*/
17+
public class Test {
18+
public static int maxSubArray(int[] nums){
19+
if(nums == null || nums.length == 0)
20+
return 0;
21+
int curSum = nums[0];
22+
int maxSum = nums[0];
23+
int starIdx = 0;
24+
int endIdx = 0;
25+
int tempIdx = 0;
26+
for (int i = 1; i < nums.length; i++){
27+
if(curSum + nums[i] > nums[i]){
28+
curSum += nums[i];
29+
}else {
30+
curSum = nums[i];
31+
tempIdx = i;
32+
}
33+
if(curSum > maxSum){
34+
maxSum = curSum;
35+
starIdx = tempIdx;
36+
endIdx = i;
37+
}
38+
39+
// curSum = Math.max(curSum + nums[i], nums[i]);
40+
// maxSum = Math.max(maxSum, curSum);
41+
}
42+
int[] res = new int[endIdx - starIdx + 1];
43+
System.arraycopy(nums, starIdx, res, 0 , endIdx - starIdx + 1);
44+
System.out.println(Arrays.toString(res));
45+
return maxSum;
46+
}
47+
48+
public static void main(String[] args){
49+
int[] nums = new int[]{-1, 1, 2, 3, -4, 0, 5, -2};
50+
int res = maxSubArray(nums);
51+
System.out.println(res);
52+
}
53+
}

python/permut/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)