Skip to content

Commit

Permalink
update codes and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dunwu committed Jul 24, 2020
1 parent 4b7399a commit de3098f
Show file tree
Hide file tree
Showing 19 changed files with 779 additions and 55 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
- [Spring](https://dunwu.github.io/spring-tutorial/) 📚
- [Spring Boot](https://dunwu.github.io/spring-boot-tutorial/) 📚
- [Spring Cloud](https://github.com/dunwu/spring-cloud-tutorial) 📚
- [Mybatis](docs/framework/mybatis.md)
- [Mybatis](docs/framework/mybatis.md) - 关键词:`SqlSession``Mapper``Executor``StatementHandler``TypeHandler``ParameterHandler``ResultSetHandler`
- [Netty](docs/soa/netty.md)

### 消息队列
Expand All @@ -37,28 +37,28 @@
>
> 消息队列主要解决应用耦合,异步消息,流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构。是大型分布式系统不可缺少的中间件。
>
> 如果想深入学习各种消息队列产品,建议先了解一下 [消息队列基本原理](https://github.com/dunwu/blog/blob/master/source/_posts/theory/mq-theory.md) ,有助于理解消息队列特性的实现和设计思路。
> 如果想深入学习各种消息队列产品,建议先了解一下 [消息队列基本原理](https://github.com/dunwu/blog/blob/master/source/_posts/theory/mq.md) ,有助于理解消息队列特性的实现和设计思路。
- [消息队列面试题](docs/mq/mq-interview.md) 💯
- [Kafka](https://github.com/dunwu/bigdata-tutorial/tree/master/docs/kafka) 📚
- [RocketMQ](docs/mq/rocketmq.md)
- [ActiveMQ](docs/mq/activemq.md)

### 缓存
### [缓存](docs/cache)

> 缓存可以说是优化系统性能的第一手段,在各种技术中都会有缓存的应用。
>
> 如果想深入学习缓存,建议先了解一下 [缓存基本原理](https://github.com/dunwu/blog/blob/master/source/_posts/theory/cache.md),有助于理解缓存的特性、原理,使用缓存常见的问题及解决方案。
![](http://dunwu.test.upcdn.net/snap/20200710163555.png)
![img](http://dunwu.test.upcdn.net/snap/20200710163555.png)

- [缓存面试题](docs/cache/cache-interview.md) 💯
- [缓存基本原理](https://github.com/dunwu/blog/blob/master/source/_posts/theory/cache.md)
- [Java 缓存框架](docs/cache/cache-framework.md) - 关键词:Spring Cache、J2Cache、jetcache
- [Java 缓存框架](docs/cache/cache-framework.md) - 关键词:Spring Cache、J2Cache、JetCache
- [Redis 教程](https://github.com/dunwu/db-tutorial/tree/master/docs/nosql/redis) 📚
- Memcached
- [Memcached 应用指南](docs/cache/memcached.md)
- [Java 缓存库](docs/cache/cache-libs.md) - 关键词:ConcurrentHashMap、LRUHashMap、Guava Cache、Caffeine、Ehcache
- [Ehcache](docs/cache/ehcache.md)
- [Ehcache 应用指南](docs/cache/ehcache.md)
- [Http 缓存](docs/cache/http-cache.md)

### 微服务
Expand Down Expand Up @@ -148,7 +148,7 @@

## 🚪 传送

◾ 🏠 [JAVACORE 首页](https://github.com/dunwu/javacore) ◾ 🎯 [我的博客](https://github.com/dunwu/blog)
◾ 🏠 [JAVATECH 首页](https://github.com/dunwu/javatech) ◾ 🎯 [我的博客](https://github.com/dunwu/blog)

> 你可能会感兴趣:
Expand Down
Binary file renamed assets/dubbo.xmind → assets/Dubbo.xmind
Binary file not shown.
Binary file renamed assets/mybatis.xmind → assets/Mybatis.xmind
Binary file not shown.
5 changes: 5 additions & 0 deletions codes/cache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<dependency>
<groupId>net.spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.12.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
package io.github.dunwu.javatech.cache;

import net.spy.memcached.CASResponse;
import net.spy.memcached.CASValue;
import net.spy.memcached.MemcachedClient;

import java.net.InetSocketAddress;
import java.util.concurrent.Future;

/**
* Memcached 客户端连接示例
*
* @author <a href="mailto:[email protected]">Zhang Peng</a>
* @since 2020-07-10
*/
public class MemcachedDemo {

public static final String URL = "127.0.0.1";
public static final int PORT = 11211;

public static void main(String[] args) {
add();
remove();
append();
prepend();
cas();
get();
delete();
incrAndDecr();
}

public static void add() {
try {

// 连接本地的 Memcached 服务
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(URL, PORT));
System.out.println("Connection to server sucessful.");

// 添加数据
Future fo = mcc.set("MyKey", 900, "Free Education");

// 打印状态
System.out.println("set status:" + fo.get());

// 输出
System.out.println("MyKey value in cache - " + mcc.get("MyKey"));

// 添加
fo = mcc.add("MyKey", 900, "memcached");

// 打印状态
System.out.println("add status:" + fo.get());

// 添加新key
fo = mcc.add("codingground", 900, "All Free Compilers");

// 打印状态
System.out.println("add status:" + fo.get());

// 输出
System.out.println("codingground value in cache - " + mcc.get("codingground"));

// 关闭连接
mcc.shutdown();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}

public static void remove() {

try {
//连接本地的 Memcached 服务
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(URL, PORT));
System.out.println("Connection to server sucessful.");

// 添加第一个 key=》value 对
Future fo = mcc.set("MyKey", 900, "Free Education");

// 输出执行 add 方法后的状态
System.out.println("add status:" + fo.get());

// 获取键对应的值
System.out.println("MyKey value in cache - " + mcc.get("MyKey"));

// 添加新的 key
fo = mcc.replace("MyKey", 900, "Largest Tutorials' Library");

// 输出执行 set 方法后的状态
System.out.println("replace status:" + fo.get());

// 获取键对应的值
System.out.println("MyKey value in cache - " + mcc.get("MyKey"));

// 关闭连接
mcc.shutdown();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}

public static void append() {

try {

// 连接本地的 Memcached 服务
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(URL, PORT));
System.out.println("Connection to server sucessful.");

// 添加数据
Future fo = mcc.set("MyKey", 900, "Free Education");

// 输出执行 set 方法后的状态
System.out.println("set status:" + fo.get());

// 获取键对应的值
System.out.println("MyKey value in cache - " + mcc.get("MyKey"));

// 对存在的key进行数据添加操作
fo = mcc.append(900, "MyKey", " for All");

// 输出执行 set 方法后的状态
System.out.println("append status:" + fo.get());

// 获取键对应的值
System.out.println("MyKey value in cache - " + mcc.get("codingground"));

// 关闭连接
mcc.shutdown();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}

public static void prepend() {

try {

// 连接本地的 Memcached 服务
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(URL, PORT));
System.out.println("Connection to server sucessful.");

// 添加数据
Future fo = mcc.set("MyKey", 900, "Education for All");

// 输出执行 set 方法后的状态
System.out.println("set status:" + fo.get());

// 获取键对应的值
System.out.println("MyKey value in cache - " + mcc.get("MyKey"));

// 对存在的key进行数据添加操作
fo = mcc.prepend(900, "MyKey", "Free ");

// 输出执行 set 方法后的状态
System.out.println("prepend status:" + fo.get());

// 获取键对应的值
System.out.println("MyKey value in cache - " + mcc.get("codingground"));

// 关闭连接
mcc.shutdown();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}

public static void cas() {

try {

// 连接本地的 Memcached 服务
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(URL, PORT));
System.out.println("Connection to server sucessful.");

// 添加数据
Future fo = mcc.set("MyKey", 900, "Free Education");

// 输出执行 set 方法后的状态
System.out.println("set status:" + fo.get());

// 使用 get 方法获取数据
System.out.println("MyKey value in cache - " + mcc.get("MyKey"));

// 通过 gets 方法获取 CAS token(令牌)
CASValue casValue = mcc.gets("MyKey");

// 输出 CAS token(令牌) 值
System.out.println("CAS token - " + casValue);

// 尝试使用cas方法来更新数据
CASResponse casresp = mcc.cas("MyKey", casValue.getCas(), 900, "Largest Tutorials-Library");

// 输出 CAS 响应信息
System.out.println("CAS Response - " + casresp);

// 输出值
System.out.println("MyKey value in cache - " + mcc.get("MyKey"));

// 关闭连接
mcc.shutdown();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}

public static void get() {

try {

// 连接本地的 Memcached 服务
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(URL, PORT));
System.out.println("Connection to server sucessful.");

// 添加数据
Future fo = mcc.set("MyKey", 900, "Free Education");

// 输出执行 set 方法后的状态
System.out.println("set status:" + fo.get());

// 使用 get 方法获取数据
System.out.println("MyKey value in cache - " + mcc.get("MyKey"));

// 关闭连接
mcc.shutdown();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}

public static void delete() {

try {

// 连接本地的 Memcached 服务
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(URL, PORT));
System.out.println("Connection to server sucessful.");

// 添加数据
Future fo = mcc.set("MyKey", 900, "World's largest online tutorials library");

// 输出执行 set 方法后的状态
System.out.println("set status:" + fo.get());

// 获取键对应的值
System.out.println("MyKey value in cache - " + mcc.get("MyKey"));

// 对存在的key进行数据添加操作
fo = mcc.delete("MyKey");

// 输出执行 delete 方法后的状态
System.out.println("delete status:" + fo.get());

// 获取键对应的值
System.out.println("MyKey value in cache - " + mcc.get("codingground"));

// 关闭连接
mcc.shutdown();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}

public static void incrAndDecr() {

try {

// 连接本地的 Memcached 服务
MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(URL, PORT));
System.out.println("Connection to server sucessful.");

// 添加数字值
Future fo = mcc.set("number", 900, "1000");

// 输出执行 set 方法后的状态
System.out.println("set status:" + fo.get());

// 获取键对应的值
System.out.println("value in cache - " + mcc.get("number"));

// 自增并输出
System.out.println("value in cache after increment - " + mcc.incr("number", 111));

// 自减并输出
System.out.println("value in cache after decrement - " + mcc.decr("number", 112));

// 关闭连接
mcc.shutdown();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}

}
3 changes: 1 addition & 2 deletions codes/javatech-file/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

Expand Down
Loading

0 comments on commit de3098f

Please sign in to comment.