-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
a1429b0
commit e9910f4
Showing
9 changed files
with
173 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.swpu.uchain.blog.dao; | ||
|
||
import com.swpu.uchain.blog.entity.Visitor; | ||
import java.util.List; | ||
|
||
public interface VisitorMapper { | ||
|
||
int updateByPrimaryKey(Visitor record); | ||
|
||
Visitor selectByPage(String page); | ||
} |
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,51 @@ | ||
package com.swpu.uchain.blog.entity; | ||
|
||
import java.io.Serializable; | ||
|
||
public class Visitor implements Serializable { | ||
private Integer id; | ||
|
||
private String page; | ||
|
||
private Long totalNum; | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public String getPage() { | ||
return page; | ||
} | ||
|
||
public void setPage(String page) { | ||
this.page = page == null ? null : page.trim(); | ||
} | ||
|
||
public Long getTotalNum() { | ||
return totalNum; | ||
} | ||
|
||
public void setTotalNum(Long totalNum) { | ||
this.totalNum = totalNum; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(getClass().getSimpleName()); | ||
sb.append(" ["); | ||
sb.append("Hash = ").append(hashCode()); | ||
sb.append(", id=").append(id); | ||
sb.append(", page=").append(page); | ||
sb.append(", totalNum=").append(totalNum); | ||
sb.append(", serialVersionUID=").append(serialVersionUID); | ||
sb.append("]"); | ||
return sb.toString(); | ||
} | ||
} |
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,12 @@ | ||
package com.swpu.uchain.blog.redis.key; | ||
|
||
/** | ||
* @author hobo | ||
* @description | ||
*/ | ||
public class IpKey extends BasePrefix{ | ||
public IpKey(int expireSeconds, String prefix) { | ||
super(expireSeconds, prefix); | ||
} | ||
public static IpKey ipKey = new IpKey(300,"Ip"); | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/swpu/uchain/blog/service/VisitorService.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,17 @@ | ||
package com.swpu.uchain.blog.service; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
/** | ||
* @author hobo | ||
* @description | ||
*/ | ||
public interface VisitorService { | ||
|
||
/** | ||
* 增加网站访客量 | ||
* @return | ||
*/ | ||
void addVisitorNum(HttpServletRequest request); | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/swpu/uchain/blog/service/impl/VisitorServiceImpl.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,41 @@ | ||
package com.swpu.uchain.blog.service.impl; | ||
|
||
import com.swpu.uchain.blog.dao.VisitorMapper; | ||
import com.swpu.uchain.blog.entity.Visitor; | ||
import com.swpu.uchain.blog.redis.RedisService; | ||
import com.swpu.uchain.blog.redis.key.IpKey; | ||
import com.swpu.uchain.blog.service.VisitorService; | ||
import com.swpu.uchain.blog.util.IpUtil; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
/** | ||
* @author hobo | ||
* @description | ||
*/ | ||
@Service | ||
public class VisitorServiceImpl implements VisitorService { | ||
|
||
|
||
@Autowired | ||
private VisitorMapper visitorMapper; | ||
|
||
@Autowired | ||
private RedisService redisService; | ||
|
||
@Override | ||
public void addVisitorNum(HttpServletRequest request) { | ||
String Ip = IpUtil.getClient(request); | ||
Visitor visitor = redisService.get(IpKey.ipKey, Ip, Visitor.class); | ||
if (visitor == null) { | ||
visitor = visitorMapper.selectByPage("total_page"); | ||
visitor.setTotalNum(visitor.getTotalNum() + 1); | ||
if (visitorMapper.updateByPrimaryKey(visitor)==1){ | ||
redisService.set(IpKey.ipKey,Ip,visitor); | ||
} | ||
|
||
} | ||
} | ||
} |
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