Skip to content

Commit

Permalink
even more
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitzer committed Jun 26, 2024
1 parent 0095181 commit ac50aee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
1 change: 0 additions & 1 deletion docs/recipes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,3 @@ How to install, congigure and create WebSockets
## [XML Fast And Easy, using SAX - Listener Functions](/docs/recipes/xml-fast-and-easy.md)

This document explains how to use XML parsing in Lucee.

9 changes: 5 additions & 4 deletions docs/recipes/loop-through-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
]
}
-->

# Looping Through File

This document explains how to handle big files in Lucee in a better way. The classic way that you are familiar with uses cffile tag, fileRead, and fileReadBinary functions to read the file into memory. This is a simple solution, but it consumes a lot of memory.
Expand All @@ -37,9 +38,9 @@ dump(label:"Array Size",var:len(arr));

In the example above,

* Read the file into the memory
* Split into array
* Loop over the array
- Read the file into the memory
- Split into array
- Loop over the array

It consumes a lot of memory.

Expand Down Expand Up @@ -68,4 +69,4 @@ In the above example, loop through the file and get each line, so in memory ther

Here you can see the above details in a video:

[Looping through Files](https://www.youtube.com/watch?v=6w2Wr8snk50)
[Looping through Files](https://www.youtube.com/watch?v=6w2Wr8snk50)
31 changes: 16 additions & 15 deletions docs/recipes/static-scope-in-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
]
}
-->

# Static scope in components

Static scope in components is needed to create an instance of cfc and call its method. It is used to avoid creating an instance each time you use the cfc.
Expand All @@ -36,7 +37,7 @@ loop query=dir {
}
```

1) Create a constructor of the component. It is the instance of the current path and also create new function hey().
1. Create a constructor of the component. It is the instance of the current path and also create new function hey().

```luceescript
// Example0.cfc
Expand All @@ -50,7 +51,7 @@ Component {
}
```

2) Next, we instantiate the component four times, and then call the hey() function. Run this example00.cfm page in the browser. It shows five dumps. Four dumps coming from inside of the constructor and the fifth dump is from hey(). Note that the init() function is private, so you cannot load it from outside the component. Therefore, you have no access to the message within init() from the cfscript in the example below.
2. Next, we instantiate the component four times, and then call the hey() function. Run this example00.cfm page in the browser. It shows five dumps. Four dumps coming from inside of the constructor and the fifth dump is from hey(). Note that the init() function is private, so you cannot load it from outside the component. Therefore, you have no access to the message within init() from the cfscript in the example below.

```luceescript
// example0.cfm
Expand All @@ -65,7 +66,7 @@ cfc.hey();

As our code gets more complicated, we need to make some additions to it.

* One option is to create the Component in the application scope or server scope, or to use the function GetComponentMetaData to store components in a more persistent manner.
- One option is to create the Component in the application scope or server scope, or to use the function GetComponentMetaData to store components in a more persistent manner.

The static scope for components was introduced in Lucee 5.0. This is a scope that is shared with all instances of the same component.

Expand All @@ -87,7 +88,7 @@ Component {

Here, the variable `counter` is defined in the static scope. This means that all instances of Example1.cfc share this variable.

2) In the following example, we call the Example1() function three times. Each time, the `counter` variable is incremented and shared across all instances.
2. In the following example, we call the Example1() function three times. Each time, the `counter` variable is incremented and shared across all instances.

```luceescript
// example1.cfm
Expand All @@ -98,7 +99,7 @@ new Example1();

## Example 3

1) Another example is using the static scope to store the result of a time-consuming operation that does not need to be recomputed every time.
1. Another example is using the static scope to store the result of a time-consuming operation that does not need to be recomputed every time.

```luceescript
// Example2.cfc
Expand All @@ -117,7 +118,7 @@ Component {

Here, the array `data` is defined in the static scope, which means it will be computed only once and shared across all instances.

2) In the following example, we call the Example2() function twice. The array `data` is computed only once and reused in the second instance.
2. In the following example, we call the Example2() function twice. The array `data` is computed only once and reused in the second instance.

```luceescript
// example2.cfm
Expand All @@ -127,7 +128,7 @@ new Example2();

## Example 4

1) The static scope can also be used for functions. In this example, we define a static function that is available to all instances.
1. The static scope can also be used for functions. In this example, we define a static function that is available to all instances.

```luceescript
// Example3.cfc
Expand All @@ -138,7 +139,7 @@ Component {
}
```

2) In the following example, we call the static function `hello` without creating an instance of Example3.
2. In the following example, we call the static function `hello` without creating an instance of Example3.

```luceescript
// example3.cfm
Expand All @@ -147,7 +148,7 @@ dump(Example3::hello());

## Example 5

1) The static scope can be used to count the number of instances created from a component.
1. The static scope can be used to count the number of instances created from a component.

```luceescript
// Example4.cfc
Expand All @@ -160,7 +161,7 @@ Component {
}
```

2) In the following example, we call the Example4() function five times. Each time the function is called, the count of `counter` in the static scope increases.
2. In the following example, we call the Example4() function five times. Each time the function is called, the count of `counter` in the static scope increases.

```luceescript
// example4.cfm
Expand All @@ -173,10 +174,10 @@ new Example4();

## Example 6

1) We can also use the static scope to store constant data like HOST, PORT.
1. We can also use the static scope to store constant data like HOST, PORT.

* If we store the instance in the variable scope, you will run into problems when you have a thousand components or it gets loaded a thousand times. This is a waste of time and memory storage.
* The static scope means that a variable only exists once and is independent of how many instances you have. So it is more memory efficient to do it that way. You can also do the same for functions.
- If we store the instance in the variable scope, you will run into problems when you have a thousand components or it gets loaded a thousand times. This is a waste of time and memory storage.
- The static scope means that a variable only exists once and is independent of how many instances you have. So it is more memory efficient to do it that way. You can also do the same for functions.

```luceescript
// Example5.cfc
Expand All @@ -198,7 +199,7 @@ Component {
}
```

2) In the following example, we call the Example5() function in two ways. It has a function splitFullName() that does not need to access anything (read or write data from the disks) and a variable scope that doesn't have to be part of the instance. It returns the firstname and lastname.
2. In the following example, we call the Example5() function in two ways. It has a function splitFullName() that does not need to access anything (read or write data from the disks) and a variable scope that doesn't have to be part of the instance. It returns the firstname and lastname.

```luceescript
// example5.cfm
Expand All @@ -211,4 +212,4 @@ dump(Example5::splitFullName("Quintana Jesus"));

[Lucee 5 features reviewed: static](https://dev.lucee.org/t/lucee-5-features-reviewed-static/433)

[Video: Lucee Static Scopes in Component Code](https://www.youtube.com/watch?v=B5ILIAbXBzo&feature=youtu.be)
[Video: Lucee Static Scopes in Component Code](https://www.youtube.com/watch?v=B5ILIAbXBzo&feature=youtu.be)

0 comments on commit ac50aee

Please sign in to comment.