Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7f426cc

Browse files
author
guangxin.yuan
committed
update
1 parent ba523eb commit 7f426cc

File tree

4 files changed

+236
-0
lines changed

4 files changed

+236
-0
lines changed

‎Rocket.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ Redis中setnx不支持设置过期时间,做分布式锁时要想避免某一
125125
1. 服务端缓存:即将热点数据缓存至服务端的内存中.(利用Redis自带的消息通知机制来保证Redis和服务端热点Key的数据一致性,对于热点Key客户端建立一个监听,当热点Key有更新操作的时候,服务端也随之更新。)
126126
2. 备份热点Key:即将热点Key+随机数,随机分配至Redis其他节点中。这样访问热点key的时候就不会全部命中到一台机器上了。
127127

128+
### Redis的RedLock算法
129+
130+
1. RedLock算法‌是由Redis的作者Salvatore Sanfilippo(也称为Antirez)提出的一种分布式锁的实现方案,旨在解决在分布式系统中实现可靠锁的问题,特别是解决单个Redis实例作为分布式锁时可能出现的单点故障问题。
131+
2. 过程1:‌获取当前时间戳‌:客户端在尝试获取锁时,首先记录当前的时间戳t0。
132+
3. 过程2:‌在多个Redis实例上尝试获取锁‌:客户端会向N个(通常建议是奇数个,如5个)独立的Redis实例发送请求,尝试获取相同的锁。每个请求都会设置相同的锁名、唯一标识符(如UUID)和过期时间。
133+
4. 过程3:‌统计获取锁成功的实例数量‌:客户端统计在多少个Redis实例上成功获取了锁。
134+
5. 过程4:‌判断锁是否获取成功‌:如果在过半数(即N/2+1)的Redis实例上成功获取了锁,并且从获取第一个锁到最后一个锁的总时间小于锁的过期时间的一半,那么认为锁获取成功。否则,认为锁获取失败,客户端需要释放已经获取的锁(如果有的话)‌
135+
128136
### 如何解决 Redis 缓存雪崩问题
129137

130138
1. 使用 Redis 高可用架构:使用 Redis 集群来保证 Redis 服务不会挂掉
@@ -834,6 +842,13 @@ Spring使用了三级缓存解决了循环依赖的问题。在populateBean()给
834842
5. 异常被 catch 捕获导致@Transactional失效。
835843
6. 数据库引擎不支持事务。
836844

845+
### @Transactional配合分布式锁的使用
846+
1. 在分布式系统中,先获取分布式锁还是先开始事务,取决于具体的应用场景和业务需求。这两种方式各有优缺点,选择哪种方式需要根据实际需求权衡。
847+
2. 先获取分布式锁,再开始事务:如果锁的持有者在事务中发生故障,可能会导致锁无法释放,从而引发死锁。如果锁的粒度过大,可能会降低系统的并发性能。
848+
3. 先获取分布式锁,再开始事务适用场景:需要严格互斥访问共享资源的场景如对数据库中的某个表或某个记录进行更新操作,操作复杂且需要多个步骤的场景
849+
4. 先开始事务,再获取分布式锁:这种方式较少见,通常用于一些特殊的场景,例如事务的执行时间非常短,或者锁的获取成本较高。需要在事务中处理锁的获取和释放逻辑,增加了实现的复杂性。如果事务在获取锁之前提交,可能会导致数据不一致。
850+
5. 先开始事务,再获取分布式锁适用场景:事务执行时间非常短的场景:例如,简单的数据库更新操作。锁的获取成本较高的场景:例如,锁服务的响应时间较长,或者锁的粒度非常细。
851+
837852
### Spring中的事务传播机制
838853

839854
1. REQUIRED(默认,常用):支持使用当前事务,如果当前事务不存在,创建一个新事务。eg:方法B用REQUIRED修饰,方法A调用方法B,如果方法A当前没有事务,方法B就新建一个事务(若还有C则B和C在各自的事务中独立执行),如果方法A有事务,方法B就加入到这个事务中,当成一个事务。

‎src/Solution.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
public class Solution {
2+
3+
// 找到旋转数组的最小值位置
4+
private static int findMinIndex(int[] nums) {
5+
int left = 0;
6+
int right = nums.length - 1;
7+
8+
while (left < right) {
9+
int mid = left + (right - left) / 2;
10+
if (nums[mid] > nums[right]) {
11+
left = mid + 1;
12+
} else {
13+
right = mid;
14+
}
15+
}
16+
return left; // 最小值的位置
17+
}
18+
19+
// 快速选择算法(类似快速排序的分区操作)
20+
private static int quickSelect(int[] nums, int start, int end, int k) {
21+
if (start == end) {
22+
return nums[start];
23+
}
24+
25+
int pivot = partition(nums, start, end);
26+
27+
if (pivot == k) {
28+
return nums[pivot];
29+
} else if (pivot > k) {
30+
return quickSelect(nums, start, pivot - 1, k);
31+
} else {
32+
return quickSelect(nums, pivot + 1, end, k);
33+
}
34+
}
35+
36+
// 快速排序的分区操作
37+
private static int partition(int[] nums, int start, int end) {
38+
int pivot = nums[end];
39+
int i = start - 1;
40+
41+
for (int j = start; j < end; j++) {
42+
if (nums[j] >= pivot) { // 从大到小排序
43+
i++;
44+
swap(nums, i, j);
45+
}
46+
}
47+
48+
swap(nums, i + 1, end);
49+
return i + 1;
50+
}
51+
52+
// 交换数组中的两个元素
53+
private static void swap(int[] nums, int i, int j) {
54+
int temp = nums[i];
55+
nums[i] = nums[j];
56+
nums[j] = temp;
57+
}
58+
59+
// 主函数:在旋转数组中找到第 k 大的数字
60+
public static int findKthLargest(int[] nums, int k) {
61+
int minIndex = findMinIndex(nums); // 找到最小值的位置
62+
int n = nums.length;
63+
64+
// 将数组分为两部分,分别查找第 k 大的数字
65+
if (k <= n - minIndex) {
66+
// 第 k 大的数字在后半部分
67+
return quickSelect(nums, minIndex, n - 1, k - 1);
68+
} else {
69+
// 第 k 大的数字在前半部分
70+
return quickSelect(nums, 0, minIndex - 1, k - (n - minIndex) - 1);
71+
}
72+
}
73+
74+
public static void main(String[] args) {
75+
int[] nums = {4, 5, 6, 7, 0, 1, 2};
76+
int k = 3;
77+
78+
int result = findKthLargest(nums, k);
79+
System.out.println("第 " + k + " 大的数字是: " + result);
80+
}
81+
}

‎src/其他/lru实现/LRUCache.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package 其他.lru实现;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class LRUCache {
7+
class DLinkedNode {
8+
int key;
9+
int value;
10+
DLinkedNode prev;
11+
DLinkedNode next;
12+
13+
public DLinkedNode() {
14+
}
15+
16+
public DLinkedNode(int _key, int _value) {
17+
key = _key;
18+
value = _value;
19+
}
20+
}
21+
22+
private Map<Integer, DLinkedNode> cache = new HashMap<>();
23+
private int size;
24+
private int capacity;
25+
private DLinkedNode head, tail;
26+
27+
public LRUCache(int capacity) {
28+
this.size = 0;
29+
this.capacity = capacity;
30+
// 使用伪头部和伪尾部节点
31+
head = new DLinkedNode();
32+
tail = new DLinkedNode();
33+
head.next = tail;
34+
tail.prev = head;
35+
}
36+
37+
public int get(int key) {
38+
DLinkedNode node = cache.get(key);
39+
if (node == null) {
40+
return -1;
41+
}
42+
// 如果 key 存在,先通过哈希表定位,再移到头部
43+
moveToHead(node);
44+
return node.value;
45+
}
46+
47+
public void put(int key, int value) {
48+
DLinkedNode node = cache.get(key);
49+
if (node == null) {
50+
// 如果 key 不存在,创建一个新的节点
51+
DLinkedNode newNode = new DLinkedNode(key, value);
52+
// 添加进哈希表
53+
cache.put(key, newNode);
54+
// 添加至双向链表的头部
55+
addToHead(newNode);
56+
++size;
57+
if (size > capacity) {
58+
// 如果超出容量,删除双向链表的尾部节点
59+
DLinkedNode tail = removeTail();
60+
// 删除哈希表中对应的项
61+
cache.remove(tail.key);
62+
--size;
63+
}
64+
} else {
65+
// 如果 key 存在,先通过哈希表定位,再修改 value,并移到头部
66+
node.value = value;
67+
moveToHead(node);
68+
}
69+
}
70+
71+
private void addToHead(DLinkedNode node) {
72+
node.prev = head;
73+
node.next = head.next;
74+
head.next.prev = node;
75+
head.next = node;
76+
}
77+
78+
private void removeNode(DLinkedNode node) {
79+
node.prev.next = node.next;
80+
node.next.prev = node.prev;
81+
}
82+
83+
private void moveToHead(DLinkedNode node) {
84+
removeNode(node);
85+
addToHead(node);
86+
}
87+
88+
private DLinkedNode removeTail() {
89+
DLinkedNode res = tail.prev;
90+
removeNode(res);
91+
return res;
92+
}
93+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package 其他.查找第k大的数字;
2+
3+
public class QuickSelect {
4+
public static int findKthLargest(int[] nums, int k) {
5+
return quickSelect(nums, 0, nums.length - 1, nums.length - k);
6+
}
7+
8+
private static int quickSelect(int[] nums, int left, int right, int kSmallest) {
9+
if (left == right) { // 如果只剩一个元素,那么它就是第k小的
10+
return nums[left];
11+
}
12+
13+
int pivotIndex = partition(nums, left, right);
14+
15+
if (kSmallest == pivotIndex) {
16+
return nums[kSmallest];
17+
} else if (kSmallest < pivotIndex) {
18+
return quickSelect(nums, left, pivotIndex - 1, kSmallest);
19+
} else {
20+
return quickSelect(nums, pivotIndex + 1, right, kSmallest);
21+
}
22+
}
23+
24+
private static int partition(int[] nums, int left, int right) {
25+
int pivot = nums[right]; // 选择最右边的元素作为pivot
26+
int i = left; // i是小于pivot的元素的最后一个位置
27+
for (int j = left; j < right; j++) {
28+
if (nums[j] < pivot) {
29+
swap(nums, i++, j);
30+
}
31+
}
32+
swap(nums, i, right); // 把pivot放到中间位置
33+
return i; // 返回pivot的正确位置
34+
}
35+
36+
private static void swap(int[] nums, int i, int j) {
37+
int temp = nums[i];
38+
nums[i] = nums[j];
39+
nums[j] = temp;
40+
}
41+
42+
public static void main(String[] args) {
43+
int[] nums = {3, 2, 1, 5, 6, 4};
44+
int k = 2; // 找第2大的数字(即第k大的数字)
45+
System.out.println("第 " + k + " 大的数字是: " + findKthLargest(nums, k)); // 输出应该是5
46+
}
47+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /