forked from ityouknow/spring-boot-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
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> | ||
|
||
<groupId>com.neo</groupId> | ||
<artifactId>spring-boot-memcache-spymemcached</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>spring-boot-memcache-spymemcached</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.0.4.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>net.spy</groupId> | ||
<artifactId>spymemcached</artifactId> | ||
<version>2.12.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
12 changes: 12 additions & 0 deletions
12
spring-boot-memcache-spymemcached/src/main/java/com/neo/MemcacheApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.neo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class MemcacheApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(MemcacheApplication.class, args); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
spring-boot-memcache-spymemcached/src/main/java/com/neo/config/MemcacheSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.neo.config; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@ConfigurationProperties(prefix = "memcache") | ||
public class MemcacheSource { | ||
|
||
private String ip; | ||
|
||
private int port; | ||
|
||
public String getIp() { | ||
return ip; | ||
} | ||
|
||
public void setIp(String ip) { | ||
this.ip = ip; | ||
} | ||
|
||
public int getPort() { | ||
return port; | ||
} | ||
|
||
public void setPort(int port) { | ||
this.port = port; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
spring-boot-memcache-spymemcached/src/main/java/com/neo/config/MemcachedRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.neo.config; | ||
|
||
import net.spy.memcached.MemcachedClient; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.Resource; | ||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
|
||
@Component | ||
public class MemcachedRunner implements CommandLineRunner { | ||
protected Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
|
||
@Resource | ||
private MemcacheSource memcacheSource; | ||
|
||
private MemcachedClient client = null; | ||
|
||
@Override | ||
public void run(String... args) throws Exception { | ||
try { | ||
client = new MemcachedClient(new InetSocketAddress(memcacheSource.getIp(),memcacheSource.getPort())); | ||
} catch (IOException e) { | ||
logger.error("inint MemcachedClient failed ",e); | ||
} | ||
} | ||
|
||
public MemcachedClient getClient() { | ||
return client; | ||
} | ||
|
||
} |
2 changes: 2 additions & 0 deletions
2
spring-boot-memcache-spymemcached/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
memcache.ip=192.168.0.161 | ||
memcache.port=11211 |
16 changes: 16 additions & 0 deletions
16
spring-boot-memcache-spymemcached/src/test/java/com/neo/MemcacheApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.neo; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class MemcacheApplicationTests { | ||
|
||
@Test | ||
public void contextLoads() { | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
spring-boot-memcache-spymemcached/src/test/java/com/neo/RepositoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.neo; | ||
|
||
import com.neo.config.MemcachedRunner; | ||
import net.spy.memcached.MemcachedClient; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import javax.annotation.Resource; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class RepositoryTests { | ||
|
||
@Resource | ||
private MemcachedRunner memcachedRunner; | ||
|
||
@Test | ||
public void testSetGet() { | ||
MemcachedClient memcachedClient = memcachedRunner.getClient(); | ||
memcachedClient.set("testkey",1000,"666666"); | ||
System.out.println("*********** "+memcachedClient.get("testkey").toString()); | ||
} | ||
|
||
} |