How to add highlights to your graduation design

Time:2020-9-27

preface

Because of the epidemic situation, I believe that many students have been preparing for the online defense recently, and so are the bloggers. From being nervous before the defense to being unable to breathe, to being relaxed after the defense, I thank all the teachers for their guidance. The whole process is like riding a roller coaster. Goodbye to my youth, my school days. In the course of defense, I am often asked: what do you think is the highlight of your project? Today, I’d like to share with you what simple technologies can be used to increase a highlight of your own design.

Send mail

In fact, spring has helped us to encapsulate the function of email sending. We just need to use it directly. In my project, we have realized the functions of email registration, forgetting password and leaving messages to users through spring mail

The first step is certainly to introduce dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

Then there is our mailservice utility class

@Service
public class MailService {

    @Autowired
    Private javamailsender mailsender; // spring encapsulated class

    private String [email protected];

    /**
    *Send mail
    */
    @Async
    public void sendMail(String title, String url, String email) {
        Simplemailmessage message = new simplemailmessage(); // it is also packaged by spring
        message.setFrom (from); // sender
        message.setSubject (title); // email title
        message.setTo (email); // mail recipient
        message.setText (URL); // email content
        mailSender.send (message); // send
    }
}

In codeJavaMailSenderas well asSimpleMailMessageThey are all encapsulated classes in spring and can be used directly.
The core code has been written, but it can’t be used at this time. You need to goObtaining authorizationTake mailbox 163 as an example
How to add highlights to your graduation design

After checking, the installation prompt will ask you to set the authorization password and so on: remember the authorized password

How to add highlights to your graduation design

After that, you just need toapplication.propertiesJust configure the file

#spring-mail
spring.mail.host=smtp.163.com
spring.mail.username=xxx @163. COM // change to your email address here
spring.mail.password=xxxx   //Authorization password
spring.mail.properties.mail.smtp.auth=truehouse
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

Next, we can test it

"Hello, I'm a giant turtle! ", " [email protected] "";

The results are as follows:

How to add highlights to your graduation design

You can see that it is very simple to send e-mail. Of course, this is only a simple function of sending e-mail. In my system, users will be required to send e-mail in theOpen the link within 15 minutes to successfully activate, otherwise the link will be invalidLet’s take a look at how this is achieved:

In fact, it is through the com.google.common The specific code is as follows:

import com.google.common.cache.Cache;


//Cache the key email key value pairs, and delete the user information from the database if the user is not activated after the expiration of 15 minutes
private final Cache<String, String> registerCache =
            CacheBuilder.newBuilder().maximumSize(100).expireAfterAccess(15, TimeUnit.MINUTES)
            .removalListener(new RemovalListener<String, String>() {
            @Override
           public void onRemoval(RemovalNotification<String, String> notification) {
                String email = notification.getValue();
                User user = new User();
                user.setEmail(email);
                List<User> targetUser = userMapper.selectUsersByQuery(user);
                if (!targetUser.isEmpty() && Objects.equal(targetUser.get(0).getEnable(), 0)) {
                    userMapper.delete (email); // code optimization: determine whether the user has been activated before deleting, and remove the inactive user
            }

        }
    }).build();

I am hereThe key email is in the cache, this key is the key sent to the user’s activation link. For example, the following is the content that will be sent to the user’s mailbox after the user clicks register, and the sukers shbe is a randomly generated key

http://127.0.0.1:8090/accounts/verify?key=sukERsSHbe

Configure some API methods to set a validity period of 15 minutes, which will be called after 15 minutesRemovalListenerMonitor, that is to say, it will executeonRemovalMethod, in this method, you can judge whether the user has been activated, if not, delete the user data.

If you don’t understand the code above, you can read it several times.

How to generate this key can be defined by myself org.apache.commons In. Lang3RandomStringUtilsClass:

String randomKey = RandomStringUtils.randomAlphabetic(10);

Ranking List

The function of sending e-mail is introduced above. Let’s take a look at the function of a leaderboard.

The effect achieved in my system is to make a real-time ranking according to the number of hits of users. At first, I wanted to add a field to the database to represent the number of hits, but I thought that the performance of accessing the database frequently would be lower, so IUsing redis to achieve LeaderboardThe function of.

We are through one of redis calledSort SetTo complete the function of the data structure, so you had better understand the relevant knowledge
Redis sortset
The code is as follows:

//Add 1 to the score of the specified ID in the redis ordered set
redisService.zincrby(HOT_HOUSE_KEY, 1.0D, id + "");
//0 represents the first element and - 1 represents the last element. Since the top 10 bits of heat are to be retained, the elements from the first to the last 11 are deleted,
//The remaining 10 are the hottest ones
redisService.zremrangeByRank(HOT_HOUSE_KEY, 0, -11);

This project is a real estate trading system, which does the ranking of popular real estate, so when the user clicks, it will call rediszincrbyTo add points to the real estate ID, redis will make a ranking according to the score. The following is the packaged reids class:

/**
     *Add points to the specified members in an ordered set
     */
    public void zincrby(String key, double sorce, String id) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.zincrby(key, sorce, id);
        } finally {
            closePool(jedis);
        }
    }

    /**
     *Remove the ordered set and specify all members within the rank range
     */
    public void zremrangeByRank(String key, long start, long end) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.zremrangeByRank(key, start, end);
        } finally {
            closePool(jedis);
        }
    }

In redis, the default is to make a ranking from low to high, and then pass thezrevrangeMethods do a reverse order to get the data, as follows:

Set<String> idSet =redisService.zrevrange(HOT_HOUSE_KEY, 0, -1);


/**
*Returns an ordered set of members within a specified interval. The positions of members are arranged in descending order of scores (from large to small)
*/
public Set<String> zrevrange(String key, long start, long end) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        Set<String> set = jedis.zrevrange(key, start, end);
        return set;
    } finally {
        closePool(jedis);
    }
}

summary

The above two functions can be regarded as a few highlights of the system, these two functions are commonly used in many systems, I hope this article can help you, if there is anything wrong, please give me more advice!