Skip to content

Commit

Permalink
Merge pull request #79 from PaladinKnightMaster/draft
Browse files Browse the repository at this point in the history
Draft
  • Loading branch information
PaladinKnightMaster authored Oct 11, 2024
2 parents 9b76673 + f3ef413 commit 07899a4
Show file tree
Hide file tree
Showing 31 changed files with 947 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Working with Numbers

## Working with Numbers
Welcome back to our **Redis course**! Now that you know how to connect to a Redis server, it's time to move forward and explore how to work with numbers in Redis. This unit builds on our previous lesson, so make sure you're comfortable establishing a connection to a Redis server.

## What You'll Learn
In this lesson, you will learn how to:

1. Set numeric values in Redis.
2. Retrieve and log numeric values.

Here's the code snippet that we'll be working with:

```JavaScript
import { createClient } from 'redis';

// Create and connect Redis client
const client = createClient({ url: 'redis://localhost:6379' });

client.on('error', (err) => console.log('Redis Client Error', err));

await client.connect();

// Setting and getting numeric values
await client.set('count', 5);
await client.set('completion_rate', 95.5);

const count = await client.get('count');
const completion_rate = await client.get('completion_rate');

console.log(`Course count: ${count}, Completion rate: ${completion_rate}`);

await client.disconnect();
```

Let's break down the code:

* We use modern ES6 `import` syntax to import the `createClient` function from the `redis` library.
* The Redis client is created with `createClient` and connected to the Redis server at `'redis://localhost:6379'`.
* The client is set to log any connection errors.
* The connection to the Redis server is established using `await client.connect()`.
* We set numeric values using the `set` method: `count` with a value of `5` and `completion_rate` with a value of `95.5`.
* We retrieve these values using the `get` method. Note that in JavaScript, the return type of the `get` method is a string, so there's no need to decode it.
* Finally, the client disconnects from the Redis server using `await client.disconnect()`.

## Why It Matters
Working with numbers in Redis is crucial because many real-world applications involve numeric data. From tracking user statistics to monitoring system performance, managing numbers in Redis allows you to perform a variety of useful operations efficiently. By mastering these basic operations with numbers, you'll be well-prepared to tackle more complex tasks and optimize your applications.

Ready to dive in? Let's move on to the practice section and get hands-on experience working with numbers in Redis!
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Working with Numbers in Redis

Great job on learning how to connect to a Redis server! Now, let's run the code you saw in the lesson to understand how to work with numbers in Redis.

We will connect to a Redis server, set numeric values, retrieve them, and print them out.

Run the following code to see how it all works together.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createClient } from 'redis';

// Create and connect Redis client
const client = createClient({ url: 'redis://localhost:6379' });

client.on('error', (err) => console.log('Redis Client Error', err));

await client.connect();

// Setting and getting numeric values
await client.set('count', 5);
await client.set('completion_rate', 95.5);

const count = await client.get('count');
const completion_rate = await client.get('completion_rate');

console.log(`Course count: ${count}, Completion rate: ${completion_rate}`);

await client.disconnect();
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Debug and Increment Redis Count

Let's build on what you just learned.

This task sets the `count` key in Redis to `5`. Then it retrieves the value of the `count` key and increments it by 1.

However, the code is not working as expected. Can you debug the code and fix the issue?
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// import { createClient } from 'redis';

// // Create and connect Redis client
// const client = createClient({ url: 'redis://localhost:6379' });

// client.on('error', (err) => console.log('Redis Client Error', err));

// await client.connect();

// await client.set('count', 5);

// let count = await client.get('count');

// console.log(`New course created. Total count: ${count + 1}`);

// await client.disconnect();

import { createClient } from 'redis';

// Create and connect Redis client
const client = createClient({ url: 'redis://localhost:6379' });

client.on('error', (err) => console.log('Redis Client Error', err));

await client.connect();

await client.set('count', 5);

let count = await client.get('count');
count = parseInt(count);

console.log(`New course created. Total count: ${count + 1}`);

await client.disconnect();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Filling Missing Parts to Work with Numbers in Redis

Great progress so far! Let's move on to practicing adding missing pieces to our Redis script.

In this task, you'll fill in the missing parts of the code to set and retrieve numeric values. Complete the code blocks where you see `TODO` comments.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// import { createClient } from 'redis';

// // Create and connect Redis client
// const client = createClient({ url: 'redis://localhost:6379' });

// client.on('error', (err) => console.log('Redis Client Error', err));

// await client.connect();

// // TODO: Set the total number of students total_students and the average grade average_grade with values 10 and 88.5, respectively

// // TODO: Retrieve the total number of students and the average grade values from Redis

// // TODO: Log the values retrieved from Redis

// await client.disconnect();

import { createClient } from 'redis';

// Create and connect Redis client
const client = createClient({ url: 'redis://localhost:6379' });

client.on('error', (err) => console.log('Redis Client Error', err));

await client.connect();

// TODO: Set the total number of students total_students and the average grade average_grade with values 10 and 88.5, respectively
client.set('total_students', 10);
client.set('average_grade', 88.5);
// TODO: Retrieve the total number of students and the average grade values from Redis
const total_students = await client.get('total_students');
const average_grade = await client.get('average_grade');
// TODO: Log the values retrieved from Redis
console.log(`Total students: ${total_students}, Average grade: ${average_grade}`);
await client.disconnect();
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Connecting to Redis and Working with Numeric Values

Great progress so far! Now let's put your knowledge into action by writing the entire code yourself.

Your task is to connect to a `Redis` server, set some numeric values, retrieve those values, and print them out.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// TODO: Import the createClient function from the 'redis' module

// TODO: Create a Redis client and connect to the Redis server at 'redis://localhost:6379'

// TODO: Handle connection errors by logging them

// TODO: Establish the connection to the Redis server

// TODO: Set numeric values for 'count' and 'completion_rate' in the Redis server with values 5 and 95.5, respectively

// TODO: Retrieve the values of 'count' and 'completion_rate' from the Redis server

// TODO: Print the retrieved values for 'count' and 'completion_rate'

// TODO: Disconnect the client from the Redis server



// TODO: Import the createClient function from the 'redis' module
import { createClient } from 'redis';
// TODO: Create a Redis client and connect to the Redis server at 'redis://localhost:6379'
const client = createClient({ url: 'redis://localhost:6379' });
// TODO: Handle connection errors by logging them
client.on('error', (err) => console.log('Redis Client Error', err));
// TODO: Establish the connection to the Redis server
await client.connect();
// TODO: Set numeric values for 'count' and 'completion_rate' in the Redis server with values 5 and 95.5, respectively
client.set('count', 5);
client.set('completion_rate', 95.5);
// TODO: Retrieve the values of 'count' and 'completion_rate' from the Redis server
const count = await client.get('count');
const completion_rate = await client.get('completion_rate');
// TODO: Print the retrieved values for 'count' and 'completion_rate'
console.log(`Course count: ${count}, Completion rate: ${completion_rate}`);
// TODO: Disconnect the client from the Redis server
await client.disconnect();
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Operations with Numbers

## Operations with Numbers
Welcome back! Now that you've learned how to work with numbers in **Redis**, it's time to build on that knowledge and explore some basic operations with these numbers. This lesson will show you how to perform operations like incrementing, decrementing, and modifying numeric values directly in Redis.

## What You'll Learn
In this lesson, you will learn how to:

1. Increment and decrement numeric values.
2. Modify numeric values using operations such as increments by a floating point.

Here's the code snippet that we'll be working with:

```JavaScript
import { createClient } from 'redis';

const client = createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
await client.connect();

// Setting and getting string values
await client.set('count', 5);
await client.set('completion_rate', 95.5);
await client.set('duration', 0);

await client.decr('count'); // 4
await client.decrBy('count', 2); // 2
await client.incr('duration'); // 1
await client.incrBy('duration', 2); // 3
await client.incrByFloat('completion_rate', 1.5); // 97

// Fetch the values and log them
const count = await client.get('count');
const duration = await client.get('duration');
const completion_rate = await client.get('completion_rate');

console.log(`Course count: ${count}`); // 2
console.log(`Duration: ${duration}`); // 3
console.log(`Completion rate: ${completion_rate}`); // 97

await client.disconnect();
```

* After setting initial values for `count`, `completion_rate`, and `duration`, we perform various operations:
* `decr` operation decrements the value of `count` by 1, and `decrBy` decrements it by the specified value, in this case, 2. So, the final value of `count` is 2.
* `incr` operation increments the value of `duration` by 1, and `incrBy` increments it by the specified value, in this case, 2. So, the final value of `duration` is 3.
* `incrByFloat` increments the value of `completion_rate` by the specified floating-point value, in this case, 1.5. So, the final value of `completion_rate` is 97.
* At the end, we fetch the updated values of `count`, `duration`, and `completion_rate` and log them to the console.

Note, that the `incr`, `decr`, `incrBy`, and `decrBy` operations cannot be applied to keys that contain floating-point values, that's why we use `incrByFloat` to increment floating-point values. Note, that in order to decrement floating-point values, you can use `incrByFloat` with a negative value.

## Why It Matters
Understanding how to perform operations with numbers in Redis is essential for real-world applications. Imagine you're building a learning management system: you would track user progress, completion rates, and time spent on courses. Redis makes it fast and easy to update these numbers in real-time.

By the end of this lesson, you'll be comfortable with basic numeric operations in Redis, preparing you for more advanced tasks. Ready to get started? Let's dive into the practice section and enhance your Redis skills!
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Running Numeric Operations in Redis

Now that you've learned how to work with numbers in Redis, let's run some code to see these operations in action.

This code will:

* Decrement the value of `count`.
* Increment the value of `completion_rate` by `1.5`.
* Increment the value of `duration`.

Simply hit the Run button to see the code in action!
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createClient } from 'redis';

const client = createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
await client.connect();

await client.set('count', 5);
await client.set('completion_rate', 95.5);
await client.set('duration', 0);

await client.decrBy('count', 1);
await client.incrByFloat('completion_rate', 1.5);
await client.incr('duration');

const count = await client.get('count');
const completion_rate = await client.get('completion_rate');
const duration = await client.get('duration');

console.log(`Course count: ${count}`);
console.log(`Completion rate: ${completion_rate}`);
console.log(`Duration: ${duration}`);

await client.disconnect();
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Decrement and Increment Operations in Redis

Great progress so far! Now, let's make our task a bit more interesting.

Modify the existing Redis operations to:

* Decrement the `completion_rate` by `2.5`.
* Increment the `duration` by `3`.

This will help you see how changing operations can affect your data.

Before you start, let's understand how the decrement by a float value works in Redis. In Redis, there is no direct command to decrement a key using a float value. However, you can use the `incrByFloat` command to decrement a key by a float value by providing a negative float value as an argument.

Here's an example:

```JavaScript
await client.incrByFloat('value', -1.1); // Decrement the value by 1.1
```

Now let's get started with the task!
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// import { createClient } from 'redis';

// const client = createClient();
// client.on('error', (err) => console.log('Redis Client Error', err));
// await client.connect();

// await client.set('count', 5);
// await client.set('completion_rate', 95.5);
// await client.set('duration', 0);

// await client.decrBy('count', 1);

// // TODO: Decrement the completion_rate by 2.5
// await client.incrByFloat('completion_rate', 1.5);

// // TODO: Increment the duration by 3.
// await client.incrBy('duration', 1);

// const count = await client.get('count');
// const completion_rate = await client.get('completion_rate');
// const duration = await client.get('duration');

// console.log(`Course count: ${count}`);
// console.log(`Completion rate: ${completion_rate}`);
// console.log(`Duration: ${duration}`);

// await client.disconnect();

import { createClient } from 'redis';

const client = createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
await client.connect();

await client.set('count', 5);
await client.set('completion_rate', 95.5);
await client.set('duration', 0);

await client.decrBy('count', 1);

// TODO: Decrement the completion_rate by 2.5
await client.incrByFloat('completion_rate', -2.5);

// TODO: Increment the duration by 3.
await client.incrBy('duration', 3);

const count = await client.get('count');
const completion_rate = await client.get('completion_rate');
const duration = await client.get('duration');

console.log(`Course count: ${count}`);
console.log(`Completion rate: ${completion_rate}`);
console.log(`Duration: ${duration}`);

await client.disconnect();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Fix the Redis Numeric Operations Bug

Now, let's identify and fix a bug in the provided code. This snippet aims to perform various operations on numeric values in `Redis`. Your goal is to correct the mistake so that the script works as intended.
Loading

0 comments on commit 07899a4

Please sign in to comment.